Write a multi threaded program that calculates the mean, median, and standard deviation for a list of integers. This program should receive a series of integers on the commandline and will then create three separate worker threads. The first thread will determine the mean value, the second will determine the median and the third will calculate the standard deviation of the integers. The variables representing the mean, median, and standard deviation 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.
MULTITHREADED STATISTICAL COMPUTATION
1. AIM
To write a multithreaded program that:

Accepts a list of integers from the command line

Creates three worker threads

Thread 1 → computes Mean

Thread 2 → computes Median

Thread 3 → computes Standard Deviation

Stores the results in global variables

Parent thread waits for all workers and then prints the results

2. THEORY
Multithreading
Multithreading allows multiple tasks to run concurrently within a single process.
In Linux, POSIX threads (pthreads) are used for creating and managing threads.

Key Functions
Function	Purpose
pthread_create()	    Creates a new thread
pthread_join()	    Waits for a thread to finish
pthread_exit()	    Terminates a thread
pthread_t	    Thread identifier
Global Variables for Shared Data
Threads can access global variables without explicit communication.
In this program:

double mean, median, stddev;
are updated by worker threads and printed by the main thread.

Statistical Definitions
Mean (Average)

Mean
=
∑
i
=
1
n
x
i
n
​​
Median

Middle value in a sorted list

If even number of elements, average of the two middle values

Standard Deviation

σ
=
∑
(
x
i
−
Mean
)
2
n

3. ALGORITHM
Parent Thread
Read integers from command line.

Store them in a global array.

Create three worker threads:

Thread 1 → calculate mean

Thread 2 → calculate median

Wait for Thread 1 to finish.

Create Thread 3 → calculate standard deviation

Wait for all threads to finish using pthread_join().

Print the computed results.

Thread 1 (Mean Calculation)
Sum all integers.

Compute mean as sum ÷ count.

Store result in the global variable mean.

Thread 2 (Median Calculation)
Sort the array.

If odd count → median = middle element

If even count → median = average of middle two

Store result in global variable median.

Thread 3 (Standard Deviation Calculation)
Subtract mean from each number.

Square the difference and sum it.

Divide by count and take square root.

Store result in stddev.

4. PROGRAM
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <math.h>

// Global variables
double mean = 0.0;
double median = 0.0;
double stddev = 0.0;

int *numbers;
int count;

//---------------------- Mean Thread -----------------------
void *calc_mean(void *arg) {
    double sum = 0.0;
    for (int i = 0; i < count; i++)
        sum += numbers[i];

    mean = sum / count;
    pthread_exit(NULL);
}

//-------------------- Median Thread -----------------------
int compare(const void *a, const void *b) {
    return (*(int *)a - *(int *)b);
}

void *calc_median(void *arg) {
    qsort(numbers, count, sizeof(int), compare);

    if (count % 2 == 0)
        median = (numbers[count/2 - 1] + numbers[count/2]) / 2.0;
    else
        median = numbers[count/2];

    pthread_exit(NULL);
}

//---------------- Std. Deviation Thread -------------------
void *calc_stddev(void *arg) {
    double variance_sum = 0.0;

    for (int i = 0; i < count; i++)
        variance_sum += pow(numbers[i] - mean, 2);

    stddev = sqrt(variance_sum / count);
    pthread_exit(NULL);
}

//--------------------------- MAIN --------------------------
int main(int argc, char *argv[]) {

    if (argc < 2) {
        printf("Usage: %s <list of integers>\n", argv[0]);
        exit(1);
    }

    count = argc - 1;
    numbers = malloc(count * sizeof(int));

    for (int i = 1; i < argc; i++)
        numbers[i-1] = atoi(argv[i]);

    pthread_t t1, t2, t3;

    pthread_create(&t1, NULL, calc_mean, NULL);
    pthread_create(&t2, NULL, calc_median, NULL);

    pthread_join(t1, NULL);
    pthread_create(&t3, NULL, calc_stddev, NULL);

    pthread_join(t2, NULL);
    pthread_join(t3, NULL);

    printf("\nResults:\n");
    printf("Mean               = %.2f\n", mean);
    printf("Median             = %.2f\n", median);
    printf("Standard Deviation = %.2f\n", stddev);

    free(numbers);
    return 0;
}
5. COMPILATION & EXECUTION
Compile
gcc multithread_stats.c -o stats -lpthread -lm
Run
./stats 10 20 30 40 50
6. SAMPLE OUTPUT
Results:
Mean               = 30.00
Median             = 30.00
Standard Deviation = 14.14
7. RESULT
A multithreaded program was successfully implemented using POSIX threads to compute the mean, median, and standard deviation of a list of integers.
Each computation was performed in a separate thread, and the parent thread printed the consolidated output.
