commit 86dadc5457a6385bac49418a93ada45fd198383d parent 1f4b3dfc4d63e89c6dc418963bc93f3199368c24 Author: Christos Margiolis <christos@margiolis.net> Date: Tue, 21 Jan 2020 15:54:07 +0200 finished combinations Diffstat:
11 files changed, 68 insertions(+), 25 deletions(-)
diff --git a/assignment-1.5-arrays-pointers-files/combinations/bin/combs b/assignment-1.5-arrays-pointers-files/combinations/bin/combs Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/combinations/include/arrhandler.h b/assignment-1.5-arrays-pointers-files/combinations/include/arrhandler.h @@ -10,4 +10,6 @@ void quicksort(int *, int, int); int partition(int *, int, int); void swap(int *, int *); -#endif- \ No newline at end of file +int find_pos(int *, int, int); + +#endif diff --git a/assignment-1.5-arrays-pointers-files/combinations/include/combinations.h b/assignment-1.5-arrays-pointers-files/combinations/include/combinations.h @@ -14,14 +14,14 @@ void x_pair(int *, int *); void y_pair(int *, int *); void print_combs(int *, int, int, int, int, int); -void combinations(int *, int *, int, int, int, int *, int *, int *, int, int, int, int); +void combinations(int *, int *, int *, int, int, int, int *, int *, int *, int, int, int, int, int); bool even_calc(int *, int, int); bool sum_comb_calc(int *, int, int); -int frequency(); +void frequency(int *, int *, int *, int); long int combinations_count(int); long double factorial(int); -void print_other(int, int, int, int); // add freq +void print_other(int, int, int, int, int *, int *); -#endif- \ No newline at end of file +#endif diff --git a/assignment-1.5-arrays-pointers-files/combinations/obj/arrhandler.o b/assignment-1.5-arrays-pointers-files/combinations/obj/arrhandler.o Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/combinations/obj/combinations.o b/assignment-1.5-arrays-pointers-files/combinations/obj/combinations.o Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/combinations/src/arrhandler.c b/assignment-1.5-arrays-pointers-files/combinations/src/arrhandler.c @@ -74,4 +74,16 @@ void swap(int *a, int *b) int temp = *a; *a = *b; *b = temp; -}- \ No newline at end of file +} + + +int find_pos(int *arr, int numIter, int val) +{ + int pos, i; + + for (i = 0; i < numIter; i++) + if (val == *(arr + i)) + pos = i; + + return pos; +} diff --git a/assignment-1.5-arrays-pointers-files/combinations/src/combinations.c b/assignment-1.5-arrays-pointers-files/combinations/src/combinations.c @@ -3,8 +3,8 @@ int get_n() { int N; - - do + + do { system("clear||cls"); printf("N (6 < N <= 49): "); @@ -44,7 +44,8 @@ void y_pair(int *y1, int *y2) void print_combs(int *arr, int N, int x1, int x2, int y1, int y2) { int *currComb = (int *)malloc(N * sizeof(int)); - int unFrstCond = 0, unScndCondOnly = 0, printed = 0; + int *freqArr = (int *)malloc(N *sizeof(int)); + int unFrstCond = 0, unScndCondOnly = 0, printed = 0; if (currComb == NULL) { @@ -55,38 +56,45 @@ void print_combs(int *arr, int N, int x1, int x2, int y1, int y2) } else { - combinations(arr, currComb, 0, N-1, 0, &printed, &unFrstCond, &unScndCondOnly, x1, x2, y1, y2); - print_other(N, unFrstCond, unScndCondOnly, printed); + combinations(arr, currComb, freqArr, 0, N-1, 0, &printed, &unFrstCond, &unScndCondOnly, x1, x2, y1, y2, N); + print_other(N, unFrstCond, unScndCondOnly, printed, arr, freqArr); } free(currComb); + free(freqArr); } -void combinations(int *arr, int *currComb, int start, int end, int index, int *printed, int *unFrstCond, int *unScndCondOnly, int x1, int x2, int y1, int y2) +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) { int i, j; if (index == COMBSN) { - for (j = 0; j < COMBSN; j++) + for (j = 0; j < COMBSN; j++) { if (even_calc(currComb, x1, x2) && sum_comb_calc(currComb, y1, y2)) { printf("%d ", *(currComb + j)); - if (j == COMBSN - 1) { (*printed)++; printf("\n"); } - } // add freq + if (j == COMBSN - 1) + { + frequency(freqArr, currComb, arr, N); + (*printed)++; + printf("\n"); + } + } } if (!even_calc(currComb, x1, x2) && sum_comb_calc(currComb, y1, y2)) (*unFrstCond)++; if (!sum_comb_calc(currComb, y1, y2)) (*unScndCondOnly)++; return; - } + } for (i = start; i <= end && end-i+1 >= COMBSN-index; i++) { *(currComb + index) = *(arr + i); - combinations(arr, currComb, i+1, end, index+1, printed, unFrstCond, unScndCondOnly, x1, x2, y1, y2); + combinations(arr, currComb, freqArr, i+1, end, index+1, printed, unFrstCond, unScndCondOnly, x1, x2, y1, y2, N); } + } @@ -112,13 +120,19 @@ bool sum_comb_calc(int *arr, int y1, int y2) } -int frequency() +void frequency(int *freqArr, int *currComb, int *arr, int N) { + int pos, i; + for (i = 0; i < COMBSN; i++) + { + pos = find_pos(arr, N, *(currComb + i)); + (*(freqArr + pos))++; + } } -long int combinations_count(int N) // wtf ??????? +long int combinations_count(int N) { return (factorial(N) / (factorial(COMBSN) * factorial(N - COMBSN))); } @@ -134,10 +148,16 @@ long double factorial(int num) } -void print_other(int N, int unFrstCond, int unScndCondOnly, int printed) +void print_other(int N, int unFrstCond, int unScndCondOnly, int printed,int *arr, int *freqArr) { - printf("\nTotal number of combinations %d to %d: %ld\n", N, COMBSN, combinations_count(N)); + int i; + + printf("\nTotal number of combinations %d to %d: %ld\n", N, COMBSN, combinations_count(N)); printf("Number of combinations not satisfying the first condition: %d\n", unFrstCond); printf("Number of combinations not satisfying the second condition only: %d\n", unScndCondOnly); - printf("Printed combinations: %d\n", printed); + printf("Printed combinations: %d\n\n", printed); + + for (i = 0; i < N; i++) + printf("%d appeared %d times\n", *(arr + i), *(freqArr + i)); + } diff --git a/assignment-1.5-arrays-pointers-files/combinations/txt/data7.txt b/assignment-1.5-arrays-pointers-files/combinations/txt/data7.txt @@ -0,0 +1,12 @@ +7 +4 +5 +10 +11 +12 +14 +13 +2 +5 +22 +200 diff --git a/assignment-1.5-arrays-pointers-files/fcombinations/bin/fcombs b/assignment-1.5-arrays-pointers-files/fcombinations/bin/fcombs Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/kcombinations/bin/combs b/assignment-1.5-arrays-pointers-files/kcombinations/bin/combs Binary files differ. diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/bin/minesweeper b/assignment-1.5-arrays-pointers-files/minesweeper/bin/minesweeper Binary files differ.