Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a multithreaded program that calculates various statistical values for a l

ID: 3599480 • Letter: W

Question

Write a multithreaded program that calculates various statistical values for a list of numbers. This program will be passed a series of numbers on the command line and will then create three separate worker threads. One thread will determine the average of the numbers, the second will determine the maximum value, and the third will determine the minimum value. For example, suppose your program is passed the integers

90 81 78 95 79 72 85

The program will report

The average value is 82

The minimum value is 72

The maximum value is 95

The variables representing the average, minimum, and maximum values will be stored globally. The worker threads will set these values, and the parent thread will output the values once the workers have exited.(We could obviously expand this program by creating additional threads that determine other statistical values, such as median and standard deviation.)

Compile the code using gcc thrd-posix.c-lpthread

Then run with ./a.out 90 81 78 95 79 72 85

Explanation / Answer

Hey User Please check the program we have made and run on our machine .

#include<stdio.h>

#include<pthread.h>

#include<stdlib.h>

int avg,min,max;

int size =0;

void *average(void *arg)

{

int *arr = (int *)arg;

int sum=0,i=0;

for(i=0;i<size;i++)

sum+=arr[i];

avg = sum/size;

pthread_exit(0);

}

void *maximum(void *arg)

{

int *arr = (int *)arg;

int i;

max= arr[0];

for(i=0;i<size;i++)

{

if(arr[i]>max)

max= arr[i];

}

pthread_exit(0);

}

void *minimum(void *arg)

{

int *arr = (int *)arg;

int i;

min =arr[0];

for(i=0;i<size;i++)

{

if(arr[i]<min)

min = arr[i];

}

pthread_exit(0);

}

int main(int argc,char *argv[])

{

int *num = (int *)malloc((argc-1)*sizeof(int));

int i;

for(i=1;i<argc;i++)

{

num[i-1] = atoi(argv[i]);

size++;

}

pthread_t tid1,tid2,tid3;;

pthread_create(&tid1,NULL,average,(void *)num);

pthread_create(&tid2,NULL,maximum,(void *)num);

pthread_create(&tid3,NULL,minimum,(void *)num);

pthread_join(tid1,NULL);

pthread_join(tid2,NULL);

pthread_join(tid3,NULL);

printf("The average value is %d ",avg);

printf("The minimum value is %d ",min);

printf("The maximum value is %d ",max);

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote