From 024d4ab239c206df90913dcc1bd955a96683b8a8 Mon Sep 17 00:00:00 2001 From: Sammy Plat Date: Thu, 11 Nov 2021 23:27:36 +0100 Subject: [PATCH 01/14] Added warnings as errors with SCons --- SConstruct | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SConstruct b/SConstruct index b5f5cdd11..d09d41e1f 100644 --- a/SConstruct +++ b/SConstruct @@ -15,7 +15,7 @@ env = Environment(ENV={'PATH': os.environ['PATH']}) env['CC'] = 'gcc' for tool in ['gcc','gnulink']: env.Tool(tool) -env['CCFLAGS'] = '' +env['CCFLAGS'] = '-Wall -Wextra -Werror' # Add other languages here when you want to add language targets # Put 'name_of_language_directory' : 'file_extension' From 2f1f2649f044453c5e20d463885ed9178c95d62d Mon Sep 17 00:00:00 2001 From: Sammy Plat Date: Thu, 11 Nov 2021 23:43:51 +0100 Subject: [PATCH 02/14] Fixed IFS --- contents/IFS/code/c/IFS.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contents/IFS/code/c/IFS.c b/contents/IFS/code/c/IFS.c index 2ba8cbdd2..99c4826be 100644 --- a/contents/IFS/code/c/IFS.c +++ b/contents/IFS/code/c/IFS.c @@ -20,7 +20,7 @@ void chaos_game(struct point *in, size_t in_n, struct point *out, struct point cur_point = {drand(), drand()}; - for (int i = 0; i < out_n; ++i) { + for (size_t i = 0; i < out_n; ++i) { out[i] = cur_point; struct point tmp = random_element(in, in_n); cur_point.x = 0.5 * (cur_point.x + tmp.x); From f974d1d771fda6de7c304b2854630a72821fbd53 Mon Sep 17 00:00:00 2001 From: Sammy Plat Date: Thu, 11 Nov 2021 23:47:01 +0100 Subject: [PATCH 03/14] Removed return-type as error for barnsley --- SConstruct | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SConstruct b/SConstruct index d09d41e1f..c51bd223d 100644 --- a/SConstruct +++ b/SConstruct @@ -15,7 +15,7 @@ env = Environment(ENV={'PATH': os.environ['PATH']}) env['CC'] = 'gcc' for tool in ['gcc','gnulink']: env.Tool(tool) -env['CCFLAGS'] = '-Wall -Wextra -Werror' +env['CCFLAGS'] = '-Wall -Wextra -Werror -Wno-error=return-type' # Add other languages here when you want to add language targets # Put 'name_of_language_directory' : 'file_extension' From 7ce4c70740ebdccc5ff30d3b52c2f2c430d0a757 Mon Sep 17 00:00:00 2001 From: Sammy Plat Date: Thu, 11 Nov 2021 23:50:41 +0100 Subject: [PATCH 04/14] Fixed Cooley-Tukey (naively) --- contents/cooley_tukey/code/c/fft.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/contents/cooley_tukey/code/c/fft.c b/contents/cooley_tukey/code/c/fft.c index 90691f373..f010f3024 100644 --- a/contents/cooley_tukey/code/c/fft.c +++ b/contents/cooley_tukey/code/c/fft.c @@ -17,7 +17,7 @@ void fft(double complex *x, int n) { fftw_execute(p); fftw_destroy_plan(p); - for (size_t i = 0; i < n; ++i) { + for (int i = 0; i < n; ++i) { x[i] = y[i] / sqrt((double)n); } } @@ -56,8 +56,8 @@ void cooley_tukey(double complex *X, const size_t N) { } void bit_reverse(double complex *X, size_t N) { - for (int i = 0; i < N; ++i) { - int n = i; + for (size_t i = 0; i < N; ++i) { + size_t n = i; int a = i; int count = (int)log2((double)N) - 1; @@ -85,7 +85,7 @@ void iterative_cooley_tukey(double complex *X, size_t N) { double complex w = cexp(-2.0 * I * M_PI / stride); for (size_t j = 0; j < N; j += stride) { double complex v = 1.0; - for (size_t k = 0; k < stride / 2; ++k) { + for (int k = 0; k < stride / 2; ++k) { X[k + j + stride / 2] = X[k + j] - v * X[k + j + stride / 2]; X[k + j] -= (X[k + j + stride / 2] - X[k + j]); v *= w; From db5b7e2aa6ab811fe32800cb65d691c9e30e030f Mon Sep 17 00:00:00 2001 From: Sammy Plat Date: Thu, 11 Nov 2021 23:52:29 +0100 Subject: [PATCH 05/14] fixed euclidean algorithm --- .../code/c/euclidean_example.c | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/contents/euclidean_algorithm/code/c/euclidean_example.c b/contents/euclidean_algorithm/code/c/euclidean_example.c index 16b0ce9ea..12892e1aa 100644 --- a/contents/euclidean_algorithm/code/c/euclidean_example.c +++ b/contents/euclidean_algorithm/code/c/euclidean_example.c @@ -1,40 +1,40 @@ #include -#include +#include int euclid_mod(int a, int b) { - a = abs(a); - b = abs(b); + a = abs(a); + b = abs(b); - while (b != 0) { - int temp = b; - b = a % b; - a = temp; - } + while (b != 0) { + int temp = b; + b = a % b; + a = temp; + } - return a; + return a; } int euclid_sub(int a, int b) { - a = abs(a); - b = abs(b); - - while (a != b) { - if (a > b) { - a -= b; - } else { - b -= a; - } + a = abs(a); + b = abs(b); + + while (a != b) { + if (a > b) { + a -= b; + } else { + b -= a; } + } - return a; + return a; } int main() { - int check1 = euclid_mod(64 * 67, 64 * 81); - int check2 = euclid_sub(128 * 12, 128 * 77); + int check1 = euclid_mod(64 * 67, 64 * 81); + int check2 = euclid_sub(128 * 12, 128 * 77); - printf("%d\n", check1); - printf("%d\n", check2); + printf("%d\n", check1); + printf("%d\n", check2); - return 0; + return 0; } From 9d80fec6541bc19ed2a174c63745621f7cb99edd Mon Sep 17 00:00:00 2001 From: Sammy Plat Date: Thu, 11 Nov 2021 23:55:12 +0100 Subject: [PATCH 06/14] fixed flood_fill (removed unused parameter in find_neighbors --- contents/flood_fill/code/c/flood_fill.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/contents/flood_fill/code/c/flood_fill.c b/contents/flood_fill/code/c/flood_fill.c index d922412a1..03df6f1fb 100644 --- a/contents/flood_fill/code/c/flood_fill.c +++ b/contents/flood_fill/code/c/flood_fill.c @@ -25,7 +25,7 @@ int inbounds(struct point p, struct canvas c) { return (p.x < 0 || p.y < 0 || p.y >= c.max_y || p.x >= c.max_x) ? 0 : 1; } -int find_neighbors(struct canvas c, struct point p, int old_val, int new_val, +int find_neighbors(struct canvas c, struct point p, int old_val, struct point *neighbors) { int cnt = 0; struct point points[4] = { @@ -90,7 +90,7 @@ void stack_fill(struct canvas c, struct point p, int old_val, int new_val) { c.data[cur_loc.x + c.max_x * cur_loc.y] = new_val; struct point neighbors[4]; - int cnt = find_neighbors(c, cur_loc, old_val, new_val, neighbors); + int cnt = find_neighbors(c, cur_loc, old_val, neighbors); for (int i = 0; i < cnt; ++i) { stack_push(&stk, neighbors[i]); @@ -160,7 +160,7 @@ void queue_fill(struct canvas c, struct point p, int old_val, int new_val) { c.data[cur_loc.x + c.max_x * cur_loc.y] = new_val; struct point neighbors[4]; - int cnt = find_neighbors(c, cur_loc, old_val, new_val, neighbors); + int cnt = find_neighbors(c, cur_loc, old_val, neighbors); for (int i = 0; i < cnt; ++i) { enqueue(&q, neighbors[i]); @@ -181,7 +181,7 @@ void recursive_fill(struct canvas c, struct point p, int old_val, c.data[p.x + c.max_x * p.y] = new_val; struct point neighbors[4]; - int cnt = find_neighbors(c, p, old_val, new_val, neighbors); + int cnt = find_neighbors(c, p, old_val, neighbors); for (int i = 0; i < cnt; ++i) { recursive_fill(c, neighbors[i], old_val, new_val); From 03dcfdef0e9bd0f86e82337e59cfd5371e53bcf5 Mon Sep 17 00:00:00 2001 From: Sammy Plat Date: Fri, 12 Nov 2021 00:01:36 +0100 Subject: [PATCH 07/14] Fixed warnings for gaussian elimination (something's wrong) --- .../code/c/gaussian_elimination.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/contents/gaussian_elimination/code/c/gaussian_elimination.c b/contents/gaussian_elimination/code/c/gaussian_elimination.c index 0840b8076..a15f44725 100644 --- a/contents/gaussian_elimination/code/c/gaussian_elimination.c +++ b/contents/gaussian_elimination/code/c/gaussian_elimination.c @@ -50,7 +50,7 @@ void gaussian_elimination(double *a, const size_t rows, const size_t cols) { void back_substitution(const double *a, double *x, const size_t rows, const size_t cols) { - for (int i = rows - 1; i >= 0; --i) { + for (size_t i = rows - 1; i == 0; --i) { double sum = 0.0; for (size_t j = cols - 2; j > i; --j) { @@ -61,17 +61,17 @@ void back_substitution(const double *a, double *x, const size_t rows, } } -void gauss_jordan(double *a, const size_t rows, const size_t cols) { - int row = 0; +void gauss_jordan(double *a, const size_t cols) { + size_t row = 0; - for (int col = 0; col < cols - 1; ++col) { + for (size_t col = 0; col < cols - 1; ++col) { if (a[row * cols + col] != 0) { - for (int i = cols - 1; i > col - 1; --i) { + for (size_t i = cols - 1; i > col - 1; --i) { a[row * cols + i] /= a[row * cols + col]; } - for (int i = 0; i < row; ++i) { - for (int j = cols - 1; j > col - 1; --j) { + for (size_t i = 0; i < row; ++i) { + for (size_t j = cols - 1; j > col - 1; --j) { a[i * cols + j] -= a[i * cols + col] * a[row * cols + j]; } } @@ -99,7 +99,7 @@ int main() { printf("\nGauss-Jordan:\n"); - gauss_jordan((double *)a, 3, 4); + gauss_jordan((double *)a, 4); for (size_t i = 0; i < 3; ++i) { printf("["); From 1c6f0863d686a365c1297aa7fa69e95bc131cd89 Mon Sep 17 00:00:00 2001 From: Sammy Plat Date: Fri, 12 Nov 2021 00:03:31 +0100 Subject: [PATCH 08/14] Fixed warnings for Huffman encoding --- contents/huffman_encoding/code/c/huffman.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/contents/huffman_encoding/code/c/huffman.c b/contents/huffman_encoding/code/c/huffman.c index 571dc8c15..4f60f5da6 100644 --- a/contents/huffman_encoding/code/c/huffman.c +++ b/contents/huffman_encoding/code/c/huffman.c @@ -125,7 +125,7 @@ struct tree* generate_tree(const char* str) { } struct heap heap = { 0 }; - for (int i = 0; i < sizeof(counts) / sizeof(int); ++i) { + for (size_t i = 0; i < sizeof(counts) / sizeof(int); ++i) { if (counts[i]) { struct tree* tree = calloc(1, sizeof(struct tree)); tree->value = (char)i; @@ -211,8 +211,6 @@ char* encode(const char* input, struct tree** huffman_tree, *codebook = generate_codebook(*huffman_tree); char* result = duplicate(get_code(codebook, *input)); - int result_length = strlen(result); - int result_capacity = result_length; input += 1; From 2bd1b85a5b3b9792cfabcf6f768b15d7ee4d1bc9 Mon Sep 17 00:00:00 2001 From: Sammy Plat Date: Fri, 12 Nov 2021 00:05:55 +0100 Subject: [PATCH 09/14] Fixed Split-Op method warnings --- contents/split-operator_method/code/c/split_op.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/contents/split-operator_method/code/c/split_op.c b/contents/split-operator_method/code/c/split_op.c index 0550e4ef2..ecf48e027 100644 --- a/contents/split-operator_method/code/c/split_op.c +++ b/contents/split-operator_method/code/c/split_op.c @@ -28,7 +28,7 @@ struct operators { double complex *wfc; }; -void fft(double complex *x, int n, bool inverse) { +void fft(double complex *x, size_t n, bool inverse) { double complex y[n]; memset(y, 0, sizeof(y)); fftw_plan p; @@ -139,8 +139,8 @@ void split_op(struct params par, struct operators opr) { sprintf(filename, "output%lu.dat", i); FILE *fp = fopen(filename, "w"); - for (int i = 0; i < opr.size; ++i) { - fprintf(fp, "%d\t%f\t%f\n", i, density[i], creal(opr.v[i])); + for (size_t i = 0; i < opr.size; ++i) { + fprintf(fp, "%ld\t%f\t%f\n", i, density[i], creal(opr.v[i])); } fclose(fp); From 321b38c0776d14280992c9c684ddc776347aeb90 Mon Sep 17 00:00:00 2001 From: Sammy Plat Date: Fri, 12 Nov 2021 00:07:54 +0100 Subject: [PATCH 10/14] Fixed stable marriage problem warnings --- contents/stable_marriage_problem/code/c/stable_marriage.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/contents/stable_marriage_problem/code/c/stable_marriage.c b/contents/stable_marriage_problem/code/c/stable_marriage.c index 546a0d5d1..e372f2492 100644 --- a/contents/stable_marriage_problem/code/c/stable_marriage.c +++ b/contents/stable_marriage_problem/code/c/stable_marriage.c @@ -20,7 +20,7 @@ void shuffle(size_t *array, size_t size) { } } -void create_group(struct person *group, size_t size, bool are_men) { +void create_group(struct person *group, size_t size) { for (size_t i = 0; i < size; ++i) { group[i].id = i; group[i].partner = NULL; @@ -85,8 +85,8 @@ int main() { struct person men[5], women[5]; - create_group(men, 5, true); - create_group(women, 5, false); + create_group(men, 5); + create_group(women, 5); for (size_t i = 0; i < 5; ++i) { printf("preferences of man %zu: ", i); From e8389dc5ceada92793bc33051ba386d7b390d9ec Mon Sep 17 00:00:00 2001 From: Sammy Plat Date: Fri, 12 Nov 2021 21:57:13 +0100 Subject: [PATCH 11/14] Removed the return-type error and reactivated error Removed useless cast to (size_t *) in stable_marriage --- SConstruct | 2 +- contents/barnsley/code/c/barnsley.c | 1 + contents/cooley_tukey/code/c/fft.c | 8 ++++---- contents/stable_marriage_problem/code/c/stable_marriage.c | 3 ++- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/SConstruct b/SConstruct index c51bd223d..d09d41e1f 100644 --- a/SConstruct +++ b/SConstruct @@ -15,7 +15,7 @@ env = Environment(ENV={'PATH': os.environ['PATH']}) env['CC'] = 'gcc' for tool in ['gcc','gnulink']: env.Tool(tool) -env['CCFLAGS'] = '-Wall -Wextra -Werror -Wno-error=return-type' +env['CCFLAGS'] = '-Wall -Wextra -Werror' # Add other languages here when you want to add language targets # Put 'name_of_language_directory' : 'file_extension' diff --git a/contents/barnsley/code/c/barnsley.c b/contents/barnsley/code/c/barnsley.c index 17119c585..db154b777 100644 --- a/contents/barnsley/code/c/barnsley.c +++ b/contents/barnsley/code/c/barnsley.c @@ -44,6 +44,7 @@ struct matrix select_array(struct matrix *hutchinson_op, double *probabilities, } rnd -= probabilities[i]; } + return hutchinson_op[0]; } // This is a general function to simulate a chaos game diff --git a/contents/cooley_tukey/code/c/fft.c b/contents/cooley_tukey/code/c/fft.c index f010f3024..f87e12afd 100644 --- a/contents/cooley_tukey/code/c/fft.c +++ b/contents/cooley_tukey/code/c/fft.c @@ -6,7 +6,7 @@ #include #include -void fft(double complex *x, int n) { +void fft(double complex *x, size_t n) { double complex y[n]; memset(y, 0, sizeof(y)); fftw_plan p; @@ -17,7 +17,7 @@ void fft(double complex *x, int n) { fftw_execute(p); fftw_destroy_plan(p); - for (int i = 0; i < n; ++i) { + for (size_t i = 0; i < n; ++i) { x[i] = y[i] / sqrt((double)n); } } @@ -81,11 +81,11 @@ void iterative_cooley_tukey(double complex *X, size_t N) { bit_reverse(X, N); for (int i = 1; i <= log2((double)N); ++i) { - int stride = pow(2, i); + size_t stride = pow(2, i); double complex w = cexp(-2.0 * I * M_PI / stride); for (size_t j = 0; j < N; j += stride) { double complex v = 1.0; - for (int k = 0; k < stride / 2; ++k) { + for (size_t k = 0; k < stride / 2; ++k) { X[k + j + stride / 2] = X[k + j] - v * X[k + j + stride / 2]; X[k + j] -= (X[k + j + stride / 2] - X[k + j]); v *= w; diff --git a/contents/stable_marriage_problem/code/c/stable_marriage.c b/contents/stable_marriage_problem/code/c/stable_marriage.c index e372f2492..e4f4ad2fd 100644 --- a/contents/stable_marriage_problem/code/c/stable_marriage.c +++ b/contents/stable_marriage_problem/code/c/stable_marriage.c @@ -24,7 +24,7 @@ void create_group(struct person *group, size_t size) { for (size_t i = 0; i < size; ++i) { group[i].id = i; group[i].partner = NULL; - group[i].prefers = (size_t*)malloc(sizeof(size_t) * size); + group[i].prefers = malloc(sizeof(size_t) * size); group[i].index = 0; for (size_t j = 0; j < size; ++j) { @@ -43,6 +43,7 @@ bool prefers_partner(size_t *prefers, size_t partner, size_t id, size_t size) { return false; } } + return true; } void stable_marriage(struct person *men, struct person *women, size_t size) { From 2745dd01956fc160aacf9e7c6076e5724fd9afe7 Mon Sep 17 00:00:00 2001 From: Sammy Plat Date: Mon, 29 Nov 2021 22:58:07 +0100 Subject: [PATCH 12/14] Updated the Gaussian Elimination code. Fixed a dumb error. --- contents/gaussian_elimination/code/c/gaussian_elimination.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contents/gaussian_elimination/code/c/gaussian_elimination.c b/contents/gaussian_elimination/code/c/gaussian_elimination.c index a15f44725..7047b444c 100644 --- a/contents/gaussian_elimination/code/c/gaussian_elimination.c +++ b/contents/gaussian_elimination/code/c/gaussian_elimination.c @@ -50,7 +50,7 @@ void gaussian_elimination(double *a, const size_t rows, const size_t cols) { void back_substitution(const double *a, double *x, const size_t rows, const size_t cols) { - for (size_t i = rows - 1; i == 0; --i) { + for (size_t i = rows - 1; i >= 0; --i) { double sum = 0.0; for (size_t j = cols - 2; j > i; --j) { From dd803972692e21442a94e83ab2a05919bd014c52 Mon Sep 17 00:00:00 2001 From: Sammy Plat Date: Mon, 29 Nov 2021 23:01:40 +0100 Subject: [PATCH 13/14] Fixed dumb mistake --- SConstruct | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SConstruct b/SConstruct index 2a6580a66..bd851cf0a 100644 --- a/SConstruct +++ b/SConstruct @@ -23,7 +23,7 @@ env = Environment(ENV=os.environ, 'Go': go_builder}, tools=['gcc', 'gnulink', 'g++', 'gas', 'gfortran']) -env['CCFLAGS'] = '-Wall -Wextra -Werror'' +env['CCFLAGS'] = '-Wall -Wextra -Werror' env['CXXFLAGS'] = '-std=c++17' env['ASFLAGS'] = '--64' From 46ca7aada27ab4c91cf2497f98b1f8a688acbf7f Mon Sep 17 00:00:00 2001 From: Sammy Plat Date: Mon, 29 Nov 2021 23:06:19 +0100 Subject: [PATCH 14/14] Fixed final warning on gaussian elimination --- .../gaussian_elimination/code/c/gaussian_elimination.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/contents/gaussian_elimination/code/c/gaussian_elimination.c b/contents/gaussian_elimination/code/c/gaussian_elimination.c index 7047b444c..6ca4ca70f 100644 --- a/contents/gaussian_elimination/code/c/gaussian_elimination.c +++ b/contents/gaussian_elimination/code/c/gaussian_elimination.c @@ -47,13 +47,13 @@ void gaussian_elimination(double *a, const size_t rows, const size_t cols) { } } -void back_substitution(const double *a, double *x, const size_t rows, - const size_t cols) { +void back_substitution(const double *a, double *x, const int rows, + const int cols) { - for (size_t i = rows - 1; i >= 0; --i) { + for (int i = rows - 1; i >= 0; --i) { double sum = 0.0; - for (size_t j = cols - 2; j > i; --j) { + for (int j = cols - 2; j > i; --j) { sum += x[j] * a[i * cols + j]; }