Write a multithreaded program that calculates various statistical values for a list of numbers: • Average • Minimum • Maximum • Median • Standard deviation This program will be passed a series of numbers on the command line and will then create three separate worker threads first. 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. After that, add two more global variables and two more threads that will determine the median and the standard deviation, respectively.
时间: 2024-01-17 10:03:32 浏览: 38
Here's a code solution in Python that uses multithreading to calculate the statistical values:
```python
import threading
import math
import sys
# Global variables to store the statistical values
average = 0
minimum = sys.maxsize
maximum = -sys.maxsize
median = 0
std_deviation = 0
# A function to calculate the average of the numbers
def calculate_average(numbers):
global average
total = sum(numbers)
average = total / len(numbers)
# A function to calculate the minimum value
def calculate_minimum(numbers):
global minimum
minimum = min(numbers)
# A function to calculate the maximum value
def calculate_maximum(numbers):
global maximum
maximum = max(numbers)
# A function to calculate the median value
def calculate_median(numbers):
global median
numbers.sort()
n = len(numbers)
if n % 2 == 0:
median = (numbers[n//2 - 1] + numbers[n//2]) / 2
else:
median = numbers[n//2]
# A function to calculate the standard deviation
def calculate_std_deviation(numbers):
global std_deviation
n = len(numbers)
mean = sum(numbers) / n
variance = sum((x - mean)**2 for x in numbers) / (n - 1)
std_deviation = math.sqrt(variance)
# The main function that creates the worker threads
def main():
numbers = [int(x) for x in sys.argv[1:]]
t1 = threading.Thread(target=calculate_average, args=(numbers,))
t2 = threading.Thread(target=calculate_minimum, args=(numbers,))
t3 = threading.Thread(target=calculate_maximum, args=(numbers,))
t4 = threading.Thread(target=calculate_median, args=(numbers,))
t5 = threading.Thread(target=calculate_std_deviation, args=(numbers,))
t1.start()
t2.start()
t3.start()
t4.start()
t5.start()
t1.join()
t2.join()
t3.join()
t4.join()
t5.join()
print("The average value is", average)
print("The minimum value is", minimum)
print("The maximum value is", maximum)
print("The median value is", median)
print("The standard deviation is", std_deviation)
if __name__ == '__main__':
main()
```
This code creates five worker threads, each responsible for calculating one of the statistical values. The `main()` function parses the command line arguments into a list of numbers, creates the worker threads, starts the threads, and then waits for them to finish. Once all the threads have finished, the main thread outputs the statistical values.