Question

File input.txt contains 100,000 numbers separated by new line. Write a C++ program to compute the...

File input.txt contains 100,000 numbers separated by new line. Write a C++ program to compute the summation of these 100,000 numbers using 10 threads, meaning each thread compute 10,000 summations and the main thread sum the result of the 10 threads.

Link to input: https://learn-us-east-1-prod-fleet01-xythos.s3.us-east-1.amazonaws.com/5b5739b6d55fd/3861301?response-content-disposition=inline%3B%20filename%2A%3DUTF-8%27%27input%25281%2529.txt&response-content-type=text%2Fplain&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20190317T013820Z&X-Amz-SignedHeaders=host&X-Amz-Expires=21600&X-Amz-Credential=AKIAIBGJ7RCS23L3LEJQ%2F20190317%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=8b1d2221f602a9dcc7b39390663d957f4fdc29f13dcf0e31e5ec15f1566b1907

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <bits/stdc++.h>
using namespace std;
#define NUMBER_OF_THREADS 10
#define TOTAL_NUMBERS 100000
int arr[TOTAL_NUMBERS];
int sum[NUMBER_OF_THREADS];
int thread_number=0;

void * find_sum(void* arg)
{
int i,j,k;
int start=(thread_number*TOTAL_NUMBERS)/NUMBER_OF_THREADS;
int en=((thread_number+1)*TOTAL_NUMBERS)/NUMBER_OF_THREADS;
for(i=start; i<en;i++)
{
sum[thread_number]+=arr[i];
}
thread_number++;
}

int main()
{
ifstream infile("sample.txt");
int n=0,number,i,j;
while(infile>>number)
{
arr[n++]=number;
}
pthread_t T[10];

// creating the threads
for(i=0;i<10;i++)
{
pthread_create(&T[i], NULL, find_sum, (void*)NULL);
}

for (j = 0; j < 10 ; j++)
{
pthread_join(T[j], NULL);
}

int answer = 0;
for (int i = 0; i < NUMBER_OF_THREADS; i++)
{
answer+= sum[i];
}

cout<<"Total sum is "<<answer;
return 0;
}

Add a comment
Know the answer?
Add Answer to:
File input.txt contains 100,000 numbers separated by new line. Write a C++ program to compute the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • File input.txt contains 100,000 numbers separated by new line. Do the following: Write a C++ program...

    File input.txt contains 100,000 numbers separated by new line. Do the following: Write a C++ program to compute the summation of these 100,000 numbers using single thread Write a C++ program to compute the summation of these 100,000 numbers using 10 threads, meaning each thread compute 10,000 summations and the main thread sum the result of the 10 threads. Using the time syscall to compare the time spent for 1.1 and 1.2 1.1 1.2 1.3 Turn Ins Source code of...

  • 1. read the article: What is the function of the human appendix? Did it once have...

    1. read the article: What is the function of the human appendix? Did it once have a purpose that has since been lost?prepared by Loren G. Martin for Scientific American. link: https://learn-us-east-1-prod-fleet01-xythos.s3.us-east-1.amazonaws.com/5dae0b35612af/1719665?response-content-disposition=inline%3B%20filename%2A%3DUTF-8%27%27What%2520is%2520the%2520function%2520of%2520the%2520human%2520appendix_%2520Did%2520it%2520once%2520have%2520a%2520purpose%2520that%2520has%2520since%2520been%2520lost_%2520-%2520Scientific%2520American.pdf&response-content-type=application%2Fpdf&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20200321T164800Z&X-Amz-SignedHeaders=host&X-Amz-Expires=21599&X-Amz-Credential=AKIAZH6WM4PLTYPZRQMY%2F20200321%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=d6ff82e79be80b81d916a34ccb3d596d36ceab723eaec8cda4fbfcd44a8af212 3. answer the following question: What is the main idea of the article? Why it is difficult to know the role of the appendix in humans? Explain briefly the function that is associated with the appendix. The appendix was cataloged as a vestigial structure, meaning it lost its...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT