IHYPRESS PROGRAMMING
Tutorials and C programs with code and output for beginners
c programming
HOME | ASP | C | CSS | GNUPLOT | HTML | JAVASCRIPT | PERL | PHP | PYTHON | RUBY | SVG
COne Dimensional Arrays : Statistical Table
<07.10>
// 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); }
Hergestellt in Deutschland / Made in Germany
Enter 8 numbers separated by spaces > 6 9 3 7 3 6 8 2 The mean is 5.50. The standard deviation is 2.40. Table of differences between data values and mean Index Item Difference 0 6.00 0.50 1 9.00 3.50 2 3.00 -2.50 3 7.00 1.50 4 3.00 -2.50 5 6.00 0.50 6 8.00 2.50 7 2.00 -3.50
COPYRIGHT © 2015-2024 IHY PRESS Frankfurt am Main 60329 Deutschland