C Source Code/Standard deviation
Appearance
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_MAX 10
/* main: main processing of standard deviation */
/* NOTE: on UNIX you may have to compile with the -lm option
* at the *end* of the command line
*/
int main(void)
{
int n, i, d, a[NUM_MAX];
double x = 0, sd, s = 0, m, sum = 0;
printf("enter how many [max %d]: ", NUM_MAX);
scanf("%d", &n);
if ( n > NUM_MAX )
{
printf("You entered a value greater than %d\n", NUM_MAX);
exit(EXIT_FAILURE);
}
for (i = 0; i < n; i++)
{
printf("enter integer element %d of %d: ", i + 1, n);
scanf("%d", &a[i]);
}
for (i = sum = 0; i < n; i++)
{
x = x + a[i];
m = x / n;
d = a[i] - m;
sum += d * d;
}
s = sum / n;
sd = sqrt(s);
printf("the number of terms = %d\n", n);
printf("their mean is m = %f\n", m);
printf("standard deviation = %f\n", sd);
/* indicate success */
exit(EXIT_SUCCESS);
}