Question

In: Computer Science

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 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.) (You can use Java or C)

Solutions

Expert Solution

For this program, we will be coding in Java as java threading API is more easy to use than PThread API in C.

Program will ask user for number of elements, input each element and displays required statistics. For taking inputs, java.util.Scanner is used as it is very convinent to use

For creating threads, there are two methods in Java

  1. Inheriting Thread class
  2. Implementing Runnable interface

Second method is used here, we create a anonymous class implementing Runnable. Doing so will help us to access global variables from threads functions.

In order to access a global variable from inner class, they have to be final. If they are final, they could not be modified. This is in case of primitive data types. In order to overcome this problem, we will be using final arrays, where we shall store the result.

After starting the threads, they have to be joined with the main thread in order for main thread to wait for them to complete execution. While doing so, the thread to be joined may throw an exception. They have to be catched and handled. Here we won't be handling the exception.

The logic for calculating statistics is easy to follow

-- code start --

import java.util.Scanner;
public class Test {

     public static void main(String []args){
        int n;
        int[] data;
        final int[] res = new int[2]; // res[0] holds min, res[1] holds max
    final double[] resf = new double[1]; // resf[0] holds avg
        Scanner s = new Scanner(System.in);
        System.out.print("Enter the number of elements :");
        n = s.nextInt();
        data = new int[n];
    System.out.println("Enter the elements :");
        for (int i = 0; i < n; i++)
            data[i] = s.nextInt();
        Thread avgThread = new Thread(new Runnable() {
            @Override
            public void run() {
        double avg = 0;
                for (int i = 0; i < n ; i++)
                    avg += data[i];
                avg = avg / n;
        resf[0] = avg;
            }
        });
        Thread minThread = new Thread(new Runnable() {
            @Override
            public void run() {
                int min = data[0];
                for (int i = 1; i < n ; i++)
                    if (min > data[i])
                        min = data[i];
                res[0] = min;
            }
        });
        Thread maxThread = new Thread(new Runnable() {
            @Override
            public void run() {
                int max = data[0];
                for (int i = 1; i < n ; i++)
                    if (max < data[i])
                        max = data[i];
                res[1] = max;
            }
        });
        avgThread.start();
        minThread.start();
        maxThread.start();
    try {
        avgThread.join();
        minThread.join();
        maxThread.join();
    }
    catch (Exception e) {
    }
        System.out.printf("Avarage of Data : %f\nMinimum of Data : %d\nMaximum of Data :%d\n", resf[0], res[0],res[1]);
     }
}

-- code end --

Sample Output


Related Solutions

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...
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...
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...
Write a function cube_all_lc(values) that takes as input a list of numbers called values, and that...
Write a function cube_all_lc(values) that takes as input a list of numbers called values, and that uses a list comprehension to create and return a list containing the cubes of the numbers in values (i.e., the numbers raised to the third power). This version of the function may not use recursion.
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 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...
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 Python program that calls a function to sum all the numbers in a list...
Write a Python program that calls a function to sum all the numbers in a list and returns the result to the caller. The main program creates a list (with hard-coded or user input) and passes the list as an argument to the function. You may not use the built-in function, sum. The program calls a second function to multiply all the numbers in a list passed to it by main and returns the product back to the caller. List...
Write a Python program that print out the list of couples of prime numbers that are...
Write a Python program that print out the list of couples of prime numbers that are less than 50, but their sum is bigger than 40. For instance(29,13)or(37,17),etc. Your program should print all couples
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT