// Computes the mean and standard deviation of an array
// displays the difference between each value and the mean.
#include <stdio.h>
#include <math.h>
#define MAX_ITEM 8
int
main (void)
{
double x[MAX_ITEM], /* data list */
mean, /* mean (average) of the data */
st_dev, /* standard deviation of the data */
sum, /* sum of the data */
sum_sqr; /* sum of the squares of the data */
int i;
// gets the data
printf("Enter %d numbers separated by spaces > ",
MAX_ITEM);
for (i = 0; i < MAX_ITEM; ++i)
scanf("%lf", &x [i]);
// computes the sum and the sum of the squares of all data
sum = 0;
sum_sqr = 0;
for (i = 0; i < MAX_ITEM; ++i) {
sum += x[i];
sum_sqr += x[i] * x[i];
}
// computes and prints the mean and standard deviation
mean = sum / MAX_ITEM;
st_dev = sqrt (sum_sqr / MAX_ITEM - mean * mean);
printf("The mean is %.2lf.\n", mean);
printf ("The standard deviation is %.2lf.\n", st_dev);
// displays the difference between each item and the mean
printf ("\nTable of differences between data values and mean\n");
printf ("Index Item Difference\n");
for (i = 0; i < MAX_ITEM; ++i)
printf ("%3d%4c%9.2lf%5c%9.2lf\n", i, ' ', x[i], ' ', x[i] - mean);
return (0);
}