/*** This is the most straightforward version. The data are read in from the keyboard and placed in an array, x, of fixed size N. The number of data points must be specified to the user and when declaring memory for x. ***/ #include <stdio.h> #include <math.h> #define N 5 /* I've decided to use 5 data points */ main() { double x[N], sigma,x_m; /* x[N] is where the numbers entered will be stored, x_m is for the mean value and sigma is for the deviation. */ int i; i = 0; /* it's a very good idea to initialise all */ sigma = 0.0; /* variables to zero at the start of the program */ x_m = 0.0; /* then you always know their correct values */ printf( "enter the %d numbers\n, N "); /* prompt the user for N numbers */ for ( i=0; i < N; i++) /* read in the N numbers and put */ { /* them in x[i] */ scanf("%lf", &x[i]); } /***** use the while loop ********/ /* this is currently in use. To try*/ i = 0; /* the for loop just comment this */ while( i < N ) /* loop out and remove the "/* */" */ { /* from around the for loop below */ x_m += x[i]; i++; /* The while loop needs a logical */ } /* condition to keep going. ie as */ x_m /= N; /* long as i<N the current x[i] is */ /* added to the value of x_m. You */ /* must remember to increment 'i' */ /***** end the while loop ********/ /***** use the for loop ********/ /* using a for loop is a bit easier */ /* for ( i=0; i < N; i++) /* since the start: i = 0 and the */ { /* increment: i++ (ie. i=i+1) are */ x_m += x[i]; /* within the for loop. */ } x_m /= N; */ /***** end the for loop ********/ i = 0; /* to use another while loop to calc */ while ( i< N ) /* the deviation you have to remember*/ { /* that i is no longer zero (after */ sigma += (x[i] -x_m)*(x[i] -x_m); /* the 1st while/for loop, so you */ i++; /* have to re-set it before starting.*/ } sigma = sqrt(sigma/N ); /* print the final results for the user. */ printf("the mean is %lf \n the standard deviation is %lf\n", x_m, sigma); }