Skip to content

Commit

Permalink
Fixing C warnings (algorithm-archivists#934)
Browse files Browse the repository at this point in the history
* Added warnings as errors for C/C++ code and fixed C warnings.
  • Loading branch information
Amaras authored Nov 29, 2021
1 parent 0aa5333 commit 71e27ba
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 54 deletions.
2 changes: 1 addition & 1 deletion SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
2 changes: 1 addition & 1 deletion contents/IFS/code/c/IFS.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions contents/barnsley/code/c/barnsley.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions contents/cooley_tukey/code/c/fft.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <time.h>
#include <fftw3.h>

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;
Expand Down Expand Up @@ -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;

Expand All @@ -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;
Expand Down
48 changes: 24 additions & 24 deletions contents/euclidean_algorithm/code/c/euclidean_example.c
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

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;
}
8 changes: 4 additions & 4 deletions contents/flood_fill/code/c/flood_fill.c
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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]);
Expand All @@ -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);
Expand Down
20 changes: 10 additions & 10 deletions contents/gaussian_elimination/code/c/gaussian_elimination.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,31 @@ 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];
}

x[i] = (a[i * cols + cols - 1] - sum) / a[i * cols + i];
}
}

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];
}
}
Expand Down Expand Up @@ -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("[");
Expand Down
4 changes: 1 addition & 3 deletions contents/huffman_encoding/code/c/huffman.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions contents/split-operator_method/code/c/split_op.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions contents/stable_marriage_problem/code/c/stable_marriage.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 71e27ba

Please sign in to comment.