diff --git a/SConstruct b/SConstruct index 222ae14c0..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'] = '' +env['CCFLAGS'] = '-Wall -Wextra -Werror' env['CXXFLAGS'] = '-std=c++17' env['ASFLAGS'] = '--64' 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); 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 90691f373..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; @@ -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; @@ -81,7 +81,7 @@ 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; 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; } 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); diff --git a/contents/gaussian_elimination/code/c/gaussian_elimination.c b/contents/gaussian_elimination/code/c/gaussian_elimination.c index 0840b8076..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 (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]; } @@ -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("["); 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; 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); diff --git a/contents/stable_marriage_problem/code/c/stable_marriage.c b/contents/stable_marriage_problem/code/c/stable_marriage.c index 546a0d5d1..e4f4ad2fd 100644 --- a/contents/stable_marriage_problem/code/c/stable_marriage.c +++ b/contents/stable_marriage_problem/code/c/stable_marriage.c @@ -20,11 +20,11 @@ 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; - 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) { @@ -85,8 +86,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);