diff --git a/C/main.c b/C/main.c
@@ -23,14 +23,14 @@
int err = SUCCESS; // Global error condition
int *sums_cache; // Shared memory between threads
+unsigned int max = 0;
// Memory structure to pass arguments to threads using the pthreads library
-struct iter_info
+typedef struct
{
int start;
- int interval;
- int max;
-};
+ int step;
+} iter_info;
// Find the number of processors on host machine
int get_num_cores()
@@ -76,10 +76,10 @@ int sum_digits(unsigned n)
return sum;
}
-int get_counterexpl_iter(struct iter_info *iter)
+int get_counterexpl_iter(iter_info *iter)
{
- for (int a = iter->start; a <= iter->max; a += iter->interval)
- for (int b = a; b <= iter->max; b++)
+ for (int a = iter->start; a <= max; a += iter->step)
+ for (int b = a; b <= max; b++)
{
if ((sums_cache[a + b] - (sums_cache[a] + sums_cache[b])) % 9 != 0)
{
@@ -95,20 +95,12 @@ int main(int argc, char *argv[])
{
if (argc > 1)
{
- // Check if argv[1] is numeric
- int i = 0;
- while (argv[1][i] != '\0')
- {
- if (argv[1][i] < '0' || argv[1][i] > '9')
- return INVALID_INPUT;
-
- i++;
- }
+ max = strtoul(argv[1], NULL, 10);
+ if (max <= 0) return INVALID_INPUT;
- unsigned int max = strtoul(argv[1], NULL, 10);
int n_threads = get_num_cores();
pthread_t thread_ids[n_threads];
- struct iter_info thread_iters[n_threads];
+ iter_info thread_iters[n_threads];
// Create the sums cache
int sums_c = 2 * max;
@@ -121,9 +113,7 @@ int main(int argc, char *argv[])
// of cores on the host machine
for (int i = 0; i < n_threads; i++)
{
- thread_iters[i].start = i;
- thread_iters[i].max = max;
- thread_iters[i].interval = n_threads;
+ thread_iters[i] = (iter_info){i, n_threads};
err = pthread_create(&thread_ids[i], NULL, get_counterexpl_iter, &thread_iters[i]);
if (err) fprintf(stderr, "Unable to create thread : %d\n", err);
@@ -139,3 +129,4 @@ int main(int argc, char *argv[])
}
else return INVALID_INPUT;
}
+