uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

combinations.c (3691B)


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