Hello everyone !!

I am trying to build a program that calculates the sum of integers between 0 and N, where N is a given integer. And I would like to do this using threads.

The program takes 2 inputs: N and the number of threads.
So if I give him successively the values 10 and 2, than the program will create 2 threads: the first one will calculate the sum from 0 to 5 and the second one will calculate the sum from 6 to 10.

As a first step, I would like that every thread calculates a portion of this sum and display it.

However, it seems that the threads I create, calculate the same portion of the total sum. And I don't really know how to fix it !!

Here is my code to show you what I did :
Code:
```#include <stdio.h>
#include <stdlib.h>

typedef struct {
long long N_min; // lower limit
long long N_max; // upper limit
} arg_t;

// function that calculates the sum of integers between lower limit and upper limit
void *routine(void* arg){
arg_t *my_arg = arg;
long long sum = 0.0;

for (long long i=my_arg->N_min; i<my_arg->N_max; i++){
sum += i ;
}
printf("portion_sum = %lli\n", sum);
}

int main(int argc, char **argv){

if (argc < 3){
printf("Usage: %s <sum length> <thread count>\n", argv);
exit(EXIT_SUCCESS);
}

if (atoi(argv) < atoi(argv)){
printf("sum length should be higher than thread count\n");
exit(EXIT_SUCCESS);
}

long long sum_length = atoi(argv)+1;
long long interval_length = sum_length / thread_count;

for (long long i=0; i<thread_count; i++){

// define the interval of focus for the routine
long long N_min = i*interval_length;
long long N_max = (i+1)*interval_length;
printf("n_min_given = %lld\n", N_min);
printf("n_max_given = %lld\n", N_max);
arg_t my_arg = {N_min, N_max};

}

for (long long i=0; i<thread_count; i++){
}
}```
can somebody help me please ? 2. Yeah, each of your my_args needs to be a unique pointer as well.

Code:
```arg_t my_args[thread_count];

for (long long i=0; i<thread_count; i++){

// define the interval of focus for the routine
long long N_min = i*interval_length;
long long N_max = (i+1)*interval_length;
printf("n_min_given = %lld\n", N_min);
printf("n_max_given = %lld\n", N_max);
my_args[i] = {N_min, N_max};

}``` 3. ok thanks !!

I still have this
Code:
```test.c: In function ‘main’:
test.c:55:18: error: expected expression before ‘{’ token
55 |     my_args[i] = {N_min, N_max};
|``` 4. It is ok now, I should set struct values like this :
Code:
```
my_args[i] = (arg_t){N_min, N_max};``` Popular pages Recent additions #include, limit, long, sum, threads 