DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Standard Deviation Calculation

08.07.2011
| 1845 views |
  • submit to reddit
        The below C program will help to calculate the Standard Deviation. 

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#define NUM_MAX 10

int main(void){
double x = 0, sd, s = 0, m, sum = 0;
int n, i, d, a[NUM_MAX];
printf("Enter How many inputs you have to calculate SD [MAX %d]: ", NUM_MAX);
scanf("%d", &n);
if ( n > NUM_MAX ){
printf("Sorry your input limit is greater than Max value
(%d)
", NUM_MAX);
exit(EXIT_FAILURE);
}
for (i = 0; i < n; i++){
printf("Enter the Integer elements %d of %d: ", i + 1, n);
scanf("%d", &a[i]);
}
for(i = sum = 0; i < n; i++){
x = x + a[i];
}
m=x/n;
for (i = sum = 0; i < n; i++){
d = a[i] - m;
sum += d * d;
}
s = sum / n;
sd = sqrt(s);//standard deviation
printf("the number of input terms = %d", n);
printf("Mean = %f", m);
printf("Standard Deviation = %f", sd);
printf("Variance = %f", s);
exit(EXIT_SUCCESS);
} 
Verify the Answer using <i><a href="http://ncalculators.com/statistics/mean-standard-deviation-calculator.htm">Standard Deviation Calculator</a></i>