Question

In: Computer Science

1. Using the pThread library, write a multithreaded program that calculates various statistical values for a...

1. Using the pThread library, 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.

Solutions

Expert Solution

#include<stdio.h>
#include<pthread.h>
int arr[50],n,i;

void *th()
{
   int sum=0;
   float average;
   printf("enter your number :=");
   scanf("%d",&n);
   printf("enetr your series:=");
  
   for(i=0;i<n;i++)
   {
       scanf("%d",&arr[i]);
   }
   for(i=0;i<n;i++)
       {
           sum=sum+arr[i];
       }
   average=sum/n;
   printf("The average value is:%f",average);
}
void *th1()
{


   int temp=arr[0];
   for(int i=1;i<n;i++)
       {
           if(temp>arr[i])
           {
           temp=arr[i];
           }
       }
   printf("\nThe Minimum value is:=%d",temp);

}
void *th2()
{

   int temp=arr[0];
   for(int i=1;i<n;i++)
       {
           if(temp<arr[i])
           {
           temp=arr[i];
           }
       }
   printf("\nThe Maximum value is:=%d",temp);
   }
int main()
{
int n,i;
pthread_t t1;
pthread_t t2;
pthread_t t3;
   n=pthread_create(&t1,NULL,&th,NULL);
   pthread_join(t1,NULL);
  
   n=pthread_create(&t2,NULL,&th1,NULL);
pthread_join(t2,NULL);
  
   n=pthread_create(&t3,NULL,&th2,NULL);
pthread_join(t3,NULL);
  

}


Related Solutions

Write a multithreaded program that calculates various statistical values for a list of numbers. This program...
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...
4. Write a multithreaded program that calculates various statistical values for a list of numbers. This...
4. 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 five separate worker threads. One thread will determine the average of the numbers, the second will determine the maximum value, the third will determine the minimum value, fourth will determine the median value and the fifth will compute the std. deviation. For example, suppose your program is passed...
Choose only one problem: 1- Write a multithreaded program that calculates various statistical values for a...
Choose only one problem: 1- 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 minimum value, and the third will determine the maximum value. The variables representing the average, minimum, and maximum values will be stored globally. The worker threads...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally,...
Write a multithreaded program in C using the pthread library and dynamic memory(malloc) that multiplies two...
Write a multithreaded program in C using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally, the result must be stored in...
Write a C++ program (using the pthread library) that accepts a phrase of unspecified length on...
Write a C++ program (using the pthread library) that accepts a phrase of unspecified length on the command line. For example: prompt$: ./vowcon Operating Systems Class at CSUN The main() in this program should read the phrase from the terminal. This phrase can be read into a global variable. This phrase or its parts can be directly accessed from the main() and from the threads. The main() has to create two threads running functions (vow and con). The main() can...
Multithreaded programming Write a multithreaded program (JAVA) that prints messages with thread IDs. 1. Create at...
Multithreaded programming Write a multithreaded program (JAVA) that prints messages with thread IDs. 1. Create at least three user-threads. 2. Each thread needs to be terminated after printing each thread ID.
Write a parallel program using Pthread based on given sequential solution. Please set the thread number...
Write a parallel program using Pthread based on given sequential solution. Please set the thread number as 10 in your code. Given Sequential Solution: #include <stdlib.h> #include <stdio.h> #include <string.h> #define MAX 10240 int total = 0; int n1,n2; char *s1,*s2; FILE *fp; int readf(FILE *fp) { if((fp=fopen("strings.txt", "r"))==NULL){ printf("ERROR: can't open string.txt!\n"); return 0; } s1=(char *)malloc(sizeof(char)*MAX); if(s1==NULL){ printf("ERROR: Out of memory!\n"); return -1; } s2=(char *)malloc(sizeof(char)*MAX); if(s2==NULL){ printf("ERROR: Out of memory\n"); return -1; } /*read s1 s2 from...
Write a program that calculates mean and standard deviation for four user entered values. The most...
Write a program that calculates mean and standard deviation for four user entered values. The most common measures of a sample are the mean and the standard deviation. the mean is the sum of the values divided by the number of elements in the data set. The dispersion - or spread in the values - is measured by the standard deviation The equation for the mean is x¯ = x1 + x2 + · · · + xn The equation...
Write a program that calculates mean and standard deviation for four user entered values. The most...
Write a program that calculates mean and standard deviation for four user entered values. The most common measures of a sample are the mean and the standard deviation. the mean is the sum of the values divided by the number of elements in the data set. The dispersion - or spread in the values - is measured by the standard deviation The equation for the mean is x¯ = x1 + x2 + · · · + xn The equation...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT