Question

Write in C Program.... Write a multi-threaded program that calculates various statistical values for a list...

Write in C Program....

Write a multi-threaded 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.

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

Note:

  • main.c file
  • command line arguement are passed by the user in snippet 1
  • global variables created
  • three threads are created
  • output is below the code

main.c

/*
Write a program in c multi-threaded 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.
*/
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>


int arr[50], i;
int n;


/*
 average, minimum, and maximum 
 values will be stored globally
*/
float average;
int minimum;
int maximum;

//thread1 for calculating average
void *th()
{
        int sum=0;
        for(i=1;i<=n;i++)
                sum=sum+arr[i];
        average=sum/n;
        
}
//thread2 for calculating minimum value
void *th1()
{


        minimum=arr[1];
        for(int i=1;i<n;i++)
                if(minimum>arr[i])
                minimum=arr[i];
        

}
//thread3 for calculating maximum value
void *th2()
{

        maximum=arr[1];
        for(int i=1;i<=n;i++)
                if(maximum<arr[i])
                        maximum=arr[i];
                        
}
int main(int argc, char *argv[])
{
    int count =0;
    /*
     a series of numbers on the command line is passing 
    */
        for (int i = 1; i < argc; i++)
        {
                arr[i] =  atoi(argv[i]);
                count++;
        }
        n = count;
        printf("%d command line arguement are passed \n", count);
        for(int i=1; i<=n; i++)
            printf("%d\t",arr[i]);
            
            
        printf("\n\n");
    int t,i;
    //three objectof worker threads are t1 ,t2 and t3
    pthread_t t1;
    pthread_t t2;
    pthread_t t3;
    
    //creating threads
        t=pthread_create(&t1,NULL,&th,NULL);
        pthread_join(t1,NULL);
    
        t=pthread_create(&t2,NULL,&th1,NULL);
        pthread_join(t2,NULL);

        t=pthread_create(&t3,NULL,&th2,NULL);
        pthread_join(t3,NULL);
        
        
    /*main is the parent thread.
    the parent thread will output the values
    once the workers have exited.
    */
    printf("The average value is %f",average);
    printf("\nThe Minimum  value is %d",minimum);
    printf("\nThe Maximum  value is %d",maximum);
        
return 0;

}

OUTPUT:

command line arguements:

c Chegg х Online C Compiler - online editor X + C onlinegdb.com/online_c_compiler# input OnlineGDB beta Command line argument

After insert the command line arguements :

C Chegg Online C Compiler - online editor X + c onlinegdb.com/online_c_compiler# 7 command line arguement are passed 90 81 78

Add a comment
Know the answer?
Add Answer to:
Write in C Program.... Write a multi-threaded program that calculates various statistical values for a list...
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
  • Multi-threaded programming help!!! Can anyone solve this problem? It has to be written in C, runs...

    Multi-threaded programming help!!! Can anyone solve this problem? It has to be written in C, runs on Linux. his time you need to rewrite a multithreaded Pthread program that works with sleep() or pthread_join() in order to print out Fibonacci sequences properly. Create a folder name, WK6 on your class Linux machine when you work. gcc assignment3.c-Wall -Werror -pthread You are required to develop your own program using C with the following information Need to declare a function that receives...

  • I want to write a C++ program which starts 2 threads. The first thread periodically generates...

    I want to write a C++ program which starts 2 threads. The first thread periodically generates a random number and adds it to the total. The second thread periodically generates a random number and subtracts it from the total. The main thread periodically monitors the value of the total. if the total exceeds the maximum value, the program terminates and alerts the user that the maximum has been exceeded. If the total undercuts the minimus value, the program terminates and...

  • C++ 3. Write a program that reads integers from a file, sums the values and calculates...

    C++ 3. Write a program that reads integers from a file, sums the values and calculates the average. a. Write a value-returning function that opens an input file named in File txt. You may "hard-code" the file name, i.e., you do not need to ask the user for the file name. The function should check the file state of the input file and return a value indicating success or failure. Main should check the returned value and if the file...

  • Write in C++ using emacs. Write a program that calculates the ending balance of several savings...

    Write in C++ using emacs. Write a program that calculates the ending balance of several savings accounts. The program reads information about different customers’ accounts from an input file, “accounts.txt”. Each row in the file contains account information for one customer. This includes: the account number, account type (premium, choice, or basic account), starting balance, total amount deposited, and total amount withdrawn. Your program should: - open the file and check for successful open, - then calculate the ending balance...

  • Write a program that reads two integer values. It then calculates and displays the sum and...

    Write a program that reads two integer values. It then calculates and displays the sum and average of all values between them, Le. if the first value is vi and the second value is 2, then the program calculates and displays the sum and average of all values in the closed range Iv1,2 Your Program must satisfy the following constraints: A. make sure that v1 is less than 2 B. Use a function named getStats(that takes two integer yalues as...

  • I need to Write a test program that prompts the user to enter 10 double values...

    I need to Write a test program that prompts the user to enter 10 double values into an array, calls a method to calculate the average of the numbers, and displays the average. Next, write a method that returns the lowest value in the array and display the lowest number. The program should then prompt the user to enter 10 integer values into an array, calculates the average, and displays the average. The program should have two overloaded methods to...

  • Write a Prolog program to find the maximum, minimum, and range of values in a list...

    Write a Prolog program to find the maximum, minimum, and range of values in a list of numbers. Please also provide the program with output.

  • Write a small JAVA program that continually reads in user values and calculates the average of all values. The program s...

    Write a small JAVA program that continually reads in user values and calculates the average of all values. The program should first ask the user for the number of values to be entered. Upon storing this value, the program should proceed to read in that amount of values. What will be the best strategy here? a suggestion would be only calculating the average after you know you have read in all values. You can expect to use some type of...

  • Using the idea of inheritance and polymorphism write a C++ program that calculates the area of...

    Using the idea of inheritance and polymorphism write a C++ program that calculates the area of regular polygons. This is a typical output when the desired program is executed: >> please enter the sides of your polygon: 3,4,5 >> Your polygon is a triangle and its area is: 6 In your program, you must check that the values entered are correct and can build a polygon. The number of the digits entered initially can help software to determine the polygon...

  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

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