Skip to content

Commit 5d9bf07

Browse files
authored
Merge pull request #568 from kvedala/cleanup
[bugs] cleanup some codes for global variables and clang-tidy specs
2 parents eb64381 + 39cdc7b commit 5d9bf07

File tree

6 files changed

+88
-27
lines changed

6 files changed

+88
-27
lines changed

.github/workflows/awesome_workflow.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ jobs:
142142
print(f"{len(space_files)} files contain space or dash characters:")
143143
print("\n".join(space_files) + "\n")
144144
145-
nodir_files = [file for file in cpp_files if file.count(os.sep) != 1]
145+
nodir_files = [file for file in cpp_files if file.count(os.sep) != 1 and "project_euler" not in file]
146146
if nodir_files:
147147
print(f"{len(nodir_files)} files are not in one and only one directory:")
148148
print("\n".join(nodir_files) + "\n")

project_euler/problem_13/sol1.c

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,23 @@ int get_number(FILE *fp, char *buffer, uint8_t *out_int)
2626
long L = strlen(buffer);
2727

2828
for (int i = 0; i < L; i++)
29+
{
2930
if (buffer[i] < 0x30 || buffer[i] > 0x39)
3031
{
3132
perror("found inavlid character in the number!");
3233
return -1;
3334
}
3435
else
36+
{
3537
out_int[L - i - 1] = buffer[i] - 0x30;
38+
}
39+
}
3640

3741
return 0;
3842
}
3943

4044
/**
41-
* Function to add arbitraty length decimal integers stored in an array.
45+
* Function to add arbitrary length decimal integers stored in an array.
4246
* a + b = c = new b
4347
*/
4448
int add_numbers(uint8_t *a, uint8_t *b, uint8_t N)
@@ -49,21 +53,25 @@ int add_numbers(uint8_t *a, uint8_t *b, uint8_t N)
4953
for (int i = 0; i < N; i++)
5054
{
5155
// printf("\t%d + %d + %d ", a[i], b[i], carry);
52-
c[i] = carry + a[i] + b[i];
53-
if (c[i] > 9) /* check for carry */
56+
c[i] = carry + a[i] + b[i]; // NOLINT // This is a known false-positive
57+
if (c[i] > 9) /* check for carry */
5458
{
5559
carry = 1;
5660
c[i] -= 10;
5761
}
5862
else
63+
{
5964
carry = 0;
65+
}
6066
// printf("= %d, %d\n", carry, c[i]);
6167
}
6268

6369
for (int i = N; i < N + 10; i++)
6470
{
6571
if (carry == 0)
72+
{
6673
break;
74+
}
6775
// printf("\t0 + %d + %d ", b[i], carry);
6876
c[i] = carry + c[i];
6977
if (c[i] > 9)
@@ -72,7 +80,9 @@ int add_numbers(uint8_t *a, uint8_t *b, uint8_t N)
7280
c[i] -= 10;
7381
}
7482
else
83+
{
7584
carry = 0;
85+
}
7686
// printf("= %d, %d\n", carry, c[i]);
7787
}
7888
return 0;
@@ -89,9 +99,13 @@ int print_number(uint8_t *number, uint8_t N, int8_t num_digits_to_print)
8999

90100
/* if end_pos < 0, print all digits */
91101
if (num_digits_to_print < 0)
102+
{
92103
end_pos = 0;
104+
}
93105
else if (num_digits_to_print <= start_pos)
106+
{
94107
end_pos = start_pos - num_digits_to_print + 1;
108+
}
95109
else
96110
{
97111
fprintf(stderr, "invalid number of digits argumet!\n");
@@ -105,26 +119,30 @@ int print_number(uint8_t *number, uint8_t N, int8_t num_digits_to_print)
105119
return 0;
106120
}
107121

108-
/** number of digits of the large number */
109-
#define N 10
110-
/** number of digits in output number */
111-
#define N2 (N + 10)
112-
113122
/** Main function */
114123
int main(void)
115124
{
116-
// const char N = 50, N2 = N+10; /* length of numbers */
117-
char txt_buffer[N + 5]; /* temporary buffer */
118-
uint8_t number[N]; /* array to store digits of a large number */
119-
uint8_t sum[N2]; /* array to store the sum of the large numbers. For
120-
safety, we make it twice the length of a number. */
125+
/* number of digits of the large number */
126+
const int N = 10;
127+
/* number of digits in output number */
128+
const int N2 = N + 10;
121129

122-
memset(sum, 0, sizeof(sum)); /* initialize sum array with 0 */
130+
// const char N = 50, N2 = N+10; /* length of numbers */
131+
char *txt_buffer =
132+
(char *)calloc(N + 5, sizeof(char)); /* temporary buffer */
133+
uint8_t *number = (uint8_t *)calloc(
134+
N, sizeof(uint8_t)); /* array to store digits of a large number */
135+
uint8_t *sum = (uint8_t *)calloc(
136+
N2, sizeof(uint8_t)); /* array to store the sum of the large
137+
numbers. For safety, we make it twice the length of a number. */
123138

124139
FILE *fp = fopen("num.txt", "rt"); /* open text file to read */
125140
if (!fp)
126141
{
127142
perror("Unable to open file 'num.txt'.");
143+
free(txt_buffer);
144+
free(sum);
145+
free(number);
128146
return -1;
129147
}
130148

@@ -134,7 +152,9 @@ int main(void)
134152
{
135153
count++;
136154
if (get_number(fp, txt_buffer, number) != 0)
155+
{
137156
break;
157+
}
138158
add_numbers(number, sum, N);
139159
} while (!feof(fp));
140160

@@ -145,5 +165,8 @@ int main(void)
145165
print_number(sum, N2, 10);
146166

147167
fclose(fp); /* close file */
168+
free(txt_buffer);
169+
free(sum);
170+
free(number);
148171
return 0;
149172
}

project_euler/problem_23/sol1.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
#include <omp.h>
1111
#endif
1212

13-
unsigned long MAX_N = 28123; /**< upper limit of numbers to check */
14-
1513
/**
1614
* Returns:
1715
* -1 if N is deficient
@@ -30,7 +28,9 @@ char get_perfect_number(unsigned long N)
3028
sum += i;
3129
unsigned long tmp = N / i;
3230
if (tmp != i)
31+
{
3332
sum += tmp;
33+
}
3434
}
3535
}
3636

@@ -56,7 +56,9 @@ unsigned long get_next_abundant(unsigned long N)
5656
{
5757
unsigned long i;
5858
for (i = N + 1; !is_abundant(i); i++)
59+
{
5960
;
61+
}
6062
return i;
6163
}
6264

@@ -74,22 +76,28 @@ char is_sum_of_abundant(unsigned long N)
7476
*/
7577
for (unsigned long i = get_next_abundant(1); i <= (N >> 1);
7678
i = get_next_abundant(i))
79+
{
7780
if (is_abundant(N - i))
7881
{
7982
#ifdef DEBUG
8083
printf("\t%4lu + %4lu = %4lu\n", i, N - i, N);
8184
#endif
8285
return 1;
8386
}
87+
}
8488
return 0;
8589
}
8690

8791
/** Main function */
8892
int main(int argc, char **argv)
8993
{
94+
unsigned long MAX_N = 28123; /* upper limit of numbers to check */
95+
9096
unsigned long sum = 0;
9197
if (argc == 2)
98+
{
9299
MAX_N = strtoul(argv[1], NULL, 10);
100+
}
93101

94102
#ifdef _OPENMP
95103
printf("Using OpenMP parallleization with %d threads\n",
@@ -107,13 +115,17 @@ int main(int argc, char **argv)
107115
{
108116
clock_t start_time = clock();
109117
if (!is_sum_of_abundant(i))
118+
{
110119
sum += i;
120+
}
111121
clock_t end_time = clock();
112122
total_duration += (double)(end_time - start_time) / CLOCKS_PER_SEC;
113123

114124
printf("... %5lu: %8lu\r", i, sum);
115125
if (i % 100 == 0)
126+
{
116127
fflush(stdout);
128+
}
117129
}
118130

119131
printf("Time taken: %.4g s\n", total_duration);

project_euler/problem_23/sol2.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
#include <omp.h>
1515
#endif
1616

17-
long MAX_N = 28123; /**< Limit of numbers to check */
18-
1917
/**
2018
* This is the global array to be used to store a flag to identify
2119
* if a particular number is abundant (1) or not (0).
@@ -42,7 +40,9 @@ char get_perfect_number(unsigned long N)
4240
sum += i;
4341
unsigned long tmp = N / i;
4442
if (tmp != i)
43+
{
4544
sum += tmp;
45+
}
4646
}
4747
}
4848

@@ -72,7 +72,9 @@ unsigned long get_next_abundant(unsigned long N)
7272
unsigned long i;
7373
/* keep checking successive numbers till an abundant number is found */
7474
for (i = N + 1; !is_abundant(i); ++i)
75+
{
7576
;
77+
}
7678
return i;
7779
}
7880

@@ -90,22 +92,28 @@ char is_sum_of_abundant(unsigned long N)
9092
*/
9193
for (unsigned long i = get_next_abundant(1); i <= (N >> 1);
9294
i = get_next_abundant(i))
95+
{
9396
if (is_abundant(N - i))
9497
{
9598
#ifdef DEBUG
9699
printf("\t%4lu + %4lu = %4lu\n", i, N - i, N);
97100
#endif
98101
return 1;
99102
}
103+
}
100104
return 0;
101105
}
102106

103107
/** Main function */
104108
int main(int argc, char **argv)
105109
{
110+
long MAX_N = 28123; /* Limit of numbers to check */
111+
106112
unsigned long sum = 0;
107113
if (argc == 2)
114+
{
108115
MAX_N = strtoul(argv[1], NULL, 10);
116+
}
109117

110118
/* byte array to store flags to identify abundant numbers
111119
* the flags are identified by bits
@@ -160,10 +168,12 @@ int main(int argc, char **argv)
160168
{
161169
clock_t start_time1 = clock();
162170
if (!is_sum_of_abundant(i))
171+
{
163172
// #ifdef _OPENMP
164173
// #pragma omp critical
165174
// #endif
166175
sum += i;
176+
}
167177
clock_t end_time1 = clock();
168178
#ifdef _OPENMP
169179
#pragma omp critical
@@ -172,7 +182,9 @@ int main(int argc, char **argv)
172182

173183
printf("... %5lu: %8lu\r", i, sum);
174184
if (i % 100 == 0)
185+
{
175186
fflush(stdout);
187+
}
176188
}
177189

178190
#ifdef DEBUG

project_euler/problem_25/sol1.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ unsigned int add_numbers(unsigned char *a, unsigned char *b, unsigned char *c,
3232
c[i] -= 10;
3333
}
3434
else
35+
{
3536
carry = 0;
37+
}
3638
// printf("= %d, %d\n", carry, c[i]);
3739
}
3840

@@ -47,7 +49,9 @@ unsigned int add_numbers(unsigned char *a, unsigned char *b, unsigned char *c,
4749
c[i] -= 10;
4850
}
4951
else
52+
{
5053
carry = 0;
54+
}
5155
// printf("= %d, %d\n", carry, c[i]);
5256
i++;
5357
}
@@ -103,7 +107,9 @@ int main(int argc, char *argv[])
103107
// putchar('\n');
104108

105109
if (digit_count == MAX_DIGITS)
110+
{
106111
break;
112+
}
107113
memcpy(fn, fn1, MAX_DIGITS);
108114
memcpy(fn1, sum, MAX_DIGITS);
109115
index++;

0 commit comments

Comments
 (0)