/* Figure 7.2 Program to Print a Table of Differences */ /* * Computes the mean and standard deviation of an array of data and displays * the difference between each value and the mean. */ #include #include #define MAX_ITEM 8 /* maximum number of items in list of data */ 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 blanks\n> ", 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 %.2f\n", mean); printf("The standard deviation is %.2f\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.2f%5c%9.2f\n", i, ' ', x[i], ' ', x[i] - mean); return (0); } /* Enter 8 numbers separated by blanks or s > 3 4 5 6 6 7 8 9 The mean is 6.00. The standard deviation is 1.87. Table of differences between data values and mean Index Item Difference 0 3.00 -3.00 1 4.00 -2.00 2 5.00 -1.00 3 6.00 0.00 4 6.00 0.00 5 7.00 1.00 6 8.00 2.00 7 9.00 3.00 */