uni

University stuff
git clone git://git.christosmarg.xyz/uni-assignments.git
Log | Files | Refs | README | LICENSE

fcombinations.c (3616B)


      1 #include "fcombinations.h"
      2 
      3 int get_n(FILE *dataFile)
      4 {
      5     int N;
      6     
      7     do
      8     {
      9         fscanf(dataFile, "%d\n", &N);
     10     } while (N <= 6 || N > 49);
     11 
     12     return N;
     13 }
     14 
     15 
     16 void x_pair(int *x1, int *x2, FILE *dataFile)
     17 {
     18     do
     19     {
     20         fscanf(dataFile, "%d\n", x1);
     21         fscanf(dataFile, "%d\n", x2);
     22     } while (*x1 < 0 || *x1 > *x2 || *x2 > 6);
     23 }
     24 
     25 
     26 void y_pair(int *y1, int *y2, FILE *dataFile)
     27 {
     28     do
     29     {
     30         fscanf(dataFile, "%d\n", y1);
     31         fscanf(dataFile, "%d\n", y2);
     32     } while (*y1 < 21 || *y1 > *y2 || *y2 > 279);
     33 }
     34 
     35 
     36 void print_combs(int *arr, int N, int x1, int x2, int y1, int y2)
     37 {
     38     int *currComb = (int *)malloc(N * sizeof(int));
     39 	int *freqArr = (int *)malloc(N *sizeof(int));
     40 	int unFrstCond = 0, unScndCondOnly = 0, printed = 0;
     41 
     42     if (currComb == NULL)
     43     {
     44         set_color(BOLD_RED);
     45         printf("Error! Not enough memory, exiting...\n");
     46         exit(EXIT_FAILURE);
     47         set_color(STANDARD);
     48     }
     49     else
     50     {    
     51         combinations(arr, currComb, freqArr, 0, N-1, 0, &printed, &unFrstCond, &unScndCondOnly, x1, x2, y1, y2, N);
     52 		print_other(N, unFrstCond, unScndCondOnly, printed, arr, freqArr);
     53     }
     54 
     55     free(currComb);
     56 	free(freqArr);
     57 }
     58 
     59 
     60 void combinations(int *arr, int *currComb, int *freqArr, int start, int end, int index, int *printed, int *unFrstCond, int *unScndCondOnly, int x1, int x2, int y1, int y2, int N) 
     61 {
     62     int i, j;
     63     
     64     if (index == COMBSN) 
     65     { 
     66 		for (j = 0; j < COMBSN; j++) 
     67         {
     68             if (even_calc(currComb, x1, x2) && sum_comb_calc(currComb, y1, y2))
     69             {
     70                 printf("%d ", *(currComb + j));
     71 				if (j == COMBSN - 1)
     72 				{
     73 					frequency(freqArr, currComb, arr, N);
     74 					(*printed)++;
     75 					printf("\n");
     76 				}
     77             }
     78         }
     79         if (!even_calc(currComb, x1, x2) && sum_comb_calc(currComb, y1, y2)) (*unFrstCond)++;
     80         if (!sum_comb_calc(currComb, y1, y2)) (*unScndCondOnly)++;
     81         return;
     82     }
     83 
     84     for (i = start; i <= end && end-i+1 >= COMBSN-index; i++) 
     85     { 
     86         *(currComb + index) = *(arr + i);
     87 		combinations(arr, currComb, freqArr, i+1, end, index+1, printed, unFrstCond, unScndCondOnly, x1, x2, y1, y2, N); 
     88     }
     89 	
     90 }
     91 
     92 
     93 bool even_calc(int *arr, int x1, int x2)
     94 {
     95     int numEven = 0, i;
     96 
     97     for (i = 0; i < COMBSN; i++)
     98         if (*(arr + i) % 2 == 0) numEven++;
     99 
    100     return (numEven >= x1 && numEven <= x2) ? true : false;
    101 }
    102 
    103 
    104 bool sum_comb_calc(int *arr, int y1, int y2)
    105 {
    106     int sumNums = 0, i;
    107 
    108     for (i = 0; i < COMBSN; i++)
    109         sumNums += *(arr + i);
    110     
    111     return (sumNums >= y1 && sumNums <= y2) ? true : false;
    112 }
    113 
    114 
    115 void frequency(int *freqArr, int *currComb, int *arr, int N)
    116 {
    117 	int pos, i;
    118 
    119 	for (i = 0; i < COMBSN; i++)
    120 	{
    121 		pos = find_pos(arr, N, *(currComb + i));
    122 		(*(freqArr + pos))++;
    123 	}
    124 }
    125 
    126 
    127 long int combinations_count(int N)
    128 {
    129     return (factorial(N) / (factorial(COMBSN) * factorial(N - COMBSN)));
    130 }
    131 
    132 
    133 long double factorial(int num)
    134 {
    135     int i;
    136     long double fac;
    137     if (num == 0) return -1;
    138     else for (i = 1, fac = 1; i <= num; i++) fac *= i;
    139     return fac;
    140 }
    141 
    142 
    143 void print_other(int N, int unFrstCond, int unScndCondOnly, int printed,int *arr, int *freqArr)
    144 {
    145     int i;
    146 	
    147 	printf("\nTotal number of combinations %d to %d: %ld\n", N, COMBSN, combinations_count(N));
    148     printf("Number of combinations not satisfying the first condition: %d\n", unFrstCond);
    149     printf("Number of combinations not satisfying the second condition only: %d\n", unScndCondOnly);
    150     printf("Printed combinations: %d\n\n", printed);
    151 
    152 	for (i = 0; i < N; i++)
    153 		printf("%d appeared %d times\n", *(arr + i), *(freqArr + i));
    154 
    155 }