0

I created two thread the job of the first thread is to sum up the array and print it out and the job of the second thread is to find the product of the array and print it out the two thread read the array from a txt file my program is hard coded I have an array size of 40 the problem is every time I run my code I get the same output even though I change some numbers in my array it should give me different value depending on every time I change any number in the array my main problem is in the part that read from a file my program works will without reading form a file

#include <stdio.h>
#include <pthread.h>
int array[40];
int sum = 0;
long long int product = 1;

//function to read file
void read_ints(const char* file_name)
{
    FILE* file = fopen(file_name, "r");
    int i = 0;
    int j = 0;
    fscanf(file, "%d", &i);
    while (!feof(file)) {
        array[j] = i;
        j++;
        fscanf(file, "%d", &i);
        fscanf(file, "%d", &i);
    }
    array[j] = i;
    fclose(file);
}

void* sum_array(void* arg)
{
    for (int i = 0; i < 40; i++)
        sum += array[i];
}

void* product_array(void* arg)
{
    for (int i = 0; i < 40; i++)
        product *= array[i];
}

int main()
{
    read_ints("input.txt");
    pthread_t threads[2];
    pthread_create(&threads[0], NULL, sum_array, (void*)NULL);

    pthread_create(&threads[1], NULL, product_array, (void*)NULL);

    for (int i = 0; i < 2; i++)
        pthread_join(threads[i], NULL);

    printf("sum is: %d \n", sum);
    printf("product is: %lli \n", product);

    return 0;
}

the following is the compilation:

gcc yyy.c -o yyy -lpthread
 ./yyy input.txt

the output I get every time is

sum is: 1022
 product is: -2987895910446399488
mch
  • 9,424
  • 2
  • 28
  • 42
32 fa
  • 1
  • 1
  • Don't post pictures of text. Program output is text that can be copy/pasted. – Jabberwocky Dec 07 '21 at 07:25
  • I post it just for extra clarification – 32 fa Dec 07 '21 at 08:32
  • It's OK to post your output,but you shoulkd post it as __text__ – Jabberwocky Dec 07 '21 at 08:34
  • I posted it as a text at the end read it carefully please – 32 fa Dec 07 '21 at 08:39
  • when you say format your compiler command properly. you should give me specific detail – 32 fa Dec 07 '21 at 08:48
  • 1
    Does this answer your question? [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – n. m. could be an AI Dec 07 '21 at 08:58
  • @32fa you can remove your comments. Now the question is properly formatted and readable. – Jabberwocky Dec 07 '21 at 09:01
  • Did you check if the array contains actually the 40 ints it is supposed to? Did you check what happens if you only create one thread? Did you check if the results are correct if you don't work with threads but if you call `sum_array` and `product_array` directly without using threads at all? You also should [edit] and post the first 4-5 lines of input.txt (formatted properly). – Jabberwocky Dec 07 '21 at 09:07
  • ... and are you sure the file has been opened correctly? You don't check if `fopen` fails. – Jabberwocky Dec 07 '21 at 09:08
  • yes I am sure to all of that. you now when I remove the read function and initialize an array of 40 element it works will and it give me output according to the numbers that has been passed on the array put the only problem is the reading from a file – 32 fa Dec 07 '21 at 09:23
  • 1
    If the only problem is the reading from a file and not threads, then you should create a [mcve] that has reading from a file but no threads. – n. m. could be an AI Dec 07 '21 at 13:08

0 Answers0