/**** * * This program computes simple statistics for numbers read from standard * input. The program first asks for the number of values that the statistics * will be computed for. The program then reads in that many values. * * The statistics computed are the sum of the numbers and the arithmetic mean. * The results are printed to standard output, in the following form: * * Sum = * Mean = * * The precise formula for the mean is defined here: * * http://www.gcseguide.co.uk/statistics_and_probability.htm * * * Author: Gene Fisher (gfisher@calpoly.edu) * Created: 14apr11 * Last Modified: 14apr11 * */ #include #include int main () { int n; /* Number of values to compute stats for */ double x; /* Input value read from the terminal */ double sum; /* Computed sum */ int i; /* Loop counter variable */ /* * Input the number of values, and prompt for the rest of the data values. */ printf("Input the number of values you want to compute stats for: "); scanf("%d", &n); printf("Input the values, separated by whitespace: "); /* * Initialize the sum to 0. */ sum = 0; /* * Compared to the while loop version of the program, the statement to * initialize the loop counter is missing here. This is because the * initialization happens in for loop initialization expression, i.e., the * first part of the for loop heading. */ /* * Loop until all the values are read in, accumulating the sum as we go. * Note that the loop will not go at all if the user enters a non-positive * value for the number of data points. */ for (i = 0; i < n; i++) { /* * Input the next value. */ scanf("%lf", &x); /* * Increment the sum. */ sum = sum + x; /* * Compared to the while loop version of the program, the statement to * increment the loop counter is missing here. This is because the * incrementing happens in for loop update expression, i.e., the third * part of the for loop heading. */ } /* * Output the results. */ printf("Sum = %f\n", sum); printf("Mean = %f\n", sum / n); return 0; }