Average & standard deviation

Description:

Gowri asks Reshmi to write a code to read n numbers (Xi) from the user and print out their average and standard deviation. Formulae are Average = (Sum of Xi)/n , Standard Deviation= Sum of (Xi *Xi)/(n*n). Help her to do this task Test Case 1 Input (stdin) 5 600 470 172 430 304 Expected Output Mean=395.20 SD=146.21 Test Case 2 Input (stdin) 5 10 1 8 7 2 Expected Output Mean=5.60 SD=3.50

Program :


#include <stdio.h>

#include <math.h>

#define MAXSIZE 10


int main()

{

    float x[MAXSIZE];

    int i, n;

    float average, variance, std_deviation, sum = 0, sum1 = 0;

    scanf("%d", &n);

    for (i = 0; i < n; i++)

    {

        scanf("%f", &x[i]);

    }

    for (i = 0; i < n; i++)

    {

        sum = sum + x[i];

    }

    average = sum / (float)n;

    for (i = 0; i < n; i++)

    {

        sum1 = sum1 + pow((x[i] - average), 2);

    }

    variance = sum1 / (float)n;

    std_deviation = sqrt(variance);

    printf("Mean=%.2f\n", average);

    printf("SD=%.2f\n", std_deviation);

return 0;

}