/* * Figure 8.3 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 kMaxItems 8 /* maximum number of items in list of data */ int main(void) { double x[kMaxItems]; /* data list */ double mean; /* mean (average) of the data */ double st_dev; /* standard deviation of the data */ double sum; /* sum of the data */ double sum_sqr; /* sum of the squares of the data */ int i; /* Gets the data */ printf("Enter %d numbers separated by blanks or s\n: ", kMaxItems); for (i = 0; i < kMaxItems; 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 < kMaxItems; i++) { sum += x[i]; sum_sqr += x[i] * x[i]; } /* Computes and prints the mean and standard deviation */ mean = sum / kMaxItems; st_dev = sqrt(sum_sqr / kMaxItems - 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 < kMaxItems; i++) { printf("%3d %9.2f %9.2f\n", i, x[i], x[i] - mean); } return (0); }