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...
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 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...
Please use Phyton to write a program: Write a program that calculates and displays the total...
Please use Phyton to write a program: Write a program that calculates and displays the total bill at a restaurant for a couple that is dining. The program should collect from the couple, cost of each meal, and the percentage of the final cost that they would like to tip. The sales tax in the state where the restaurant exists is 7.5%. Display to the user, line by line: Total Cost of Both Meals Sales Tax in dollars Tip in...
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
8.26 LAB: Output numbers in reverse Write a program that reads a list of integers, and...
8.26 LAB: Output numbers in reverse Write a program that reads a list of integers, and outputs those integers in reverse. The input begins with an integer indicating the number of integers that follow. For coding simplicity, follow each output integer by a space, including the last one. Assume that the list will always contain fewer than 20 integers. Ex: If the input is: 5 2 4 6 8 10 the output is: 10 8 6 4 2 To achieve...
Write a program that calculates the balance of a savings account at the end of a...
Write a program that calculates the balance of a savings account at the end of a three month period. It should ask the user for the starting balance and the annual interest rate. A loop should then iterate once for every month in the period, performing the following: Ask the user for the total amount deposited into the account during that month. Do not accept negative numbers. This amount should be added to the balance. Ask the user for the...
Write a program that calculates the balance of a savings account at the end of a...
Write a program that calculates the balance of a savings account at the end of a period of time. It should ask the user for the annual interest rate, the starting balance, and the number of months that have passed since the account was established. A loop should then iterate once for every month, performing the following: Ask the user for the amount deposited into the account during the month. (Do not accept negative numbers.) This amount should be added...
Write a program to find out the maximum value out of a list of ten values...
Write a program to find out the maximum value out of a list of ten values using loop. The maximum finding codes can be in a Sub Routine named MAX (optional). easy68k assembly language
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT