Write a C program that finds the sum of all the elements in an array. You have t
ID: 3804043 • Letter: W
Question
Write a C program that finds the sum of all the elements in an array. You have to implement 3 versions of the program. The program will have an integer array of 20,000 elements. Populate the array using random values between -100 and 100. a) The first version of the program is single threaded. Show the sum, and also the time required to execute the program. b) The second version of the program has 5 threads. Appropriately divide the summation work among the threads, and then use the main thread to find the sum from the partial sums created by the threads. Show the sum and the time required to execute the program. c) The third version of the program is similar to the second version, except it uses 10 threads. Write the program in C, and use pthreads to perform the required actions. Also include a print screen of the execution of the program in addition to the source code.Explanation / Answer
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <pthread.h>
int sum = 0;
int randomgen(int max,int min);
int findSum(int arr[],int size);
int main(){
clock_t t;
t = clock();
int arr[20000];
int i;
pthread_t tid;
for (i=0;i <20000;i++){
arr[i] = randomgen(100,-100);
}
for (i = 0; i < 5; i++){
pthread_create(&tid, NULL, findSum, arr);
}
pthread_exit(NULL);
printf("The sum is : %d ",sum);
t = clock() -t;
double time_taken = ((double) t)/CLOCKS_PER_SEC;
printf ("the program took %f seconds to execute ",time_taken);
}
int randomgen(int max, int min) //Pass in range
{
srand(time(NULL)); //Changed from rand(). srand() seeds rand for you.
int random = rand() % max + min;
return random;
}
void *findSum (void *varg){
int arr[20000] = (int [])varg;
int i;
for (i = 0;i <20000;i++){
sum = sum+arr[i];
}
return NULL;
}
-------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <pthread.h>
int sum = 0;
int randomgen(int max,int min);
void *findSum(void *varg);
int main(){
clock_t t;
t = clock();
int arr[20000];
int i;
pthread_t tid;
for (i=0;i <20000;i++){
arr[i] = randomgen(100,-100);
}
for (i = 0; i < 5; i++){
pthread_create(&tid, NULL, findSum, arr);
}
pthread_exit(NULL);
printf("The sum is : %d ",sum);
t = clock() -t;
double time_taken = ((double) t)/CLOCKS_PER_SEC;
printf ("the program took %f seconds to execute ",time_taken);
}
int randomgen(int max, int min) //Pass in range
{
srand(time(NULL)); //Changed from rand(). srand() seeds rand for you.
int random = rand() % max + min;
return random;
}
void *findSum (void *varg){
int *arr = (int *)varg;
int i;
for (i = 0;i <20000;i++){
sum = sum+arr[i];
}
return NULL;
}
------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <pthread.h>
int sum = 0;
int randomgen(int max,int min);
void *findSum(void *varg);
int main(){
clock_t t;
t = clock();
int arr[20000];
int i;
pthread_t tid;
for (i=0;i <20000;i++){
arr[i] = randomgen(100,-100);
}
for (i = 0; i < 10; i++){
pthread_create(&tid, NULL, findSum, arr);
}
pthread_exit(NULL);
printf("The sum is : %d ",sum);
t = clock() -t;
double time_taken = ((double) t)/CLOCKS_PER_SEC;
printf ("the program took %f seconds to execute ",time_taken);
}
int randomgen(int max, int min) //Pass in range
{
srand(time(NULL)); //Changed from rand(). srand() seeds rand for you.
int random = rand() % max + min;
return random;
}
void *findSum (void *varg){
int *arr = (int *)varg;
int i;
for (i = 0;i <20000;i++){
sum = sum+arr[i];
}
return NULL;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.