Write a C Program for Mean,Variance & Standard deviation of n numbers
Here in this c program, we need to find out mean variance and standard deviation of n numbers, for that we need to know what is meant by mean, standard deviation and variance.
Mean : it is the average of a number of elements in a set of values. which means just add the values in a set and divide the sum with the number of elements in the set.
Variance : It is the average of squared difference of mean and the values of the data set it is called the spread of data within a sample of data.
Standard deviation : after we got the variance we get the standard deviation by taking square root of the variance
In this c program we are taking the values for a set from the users, the mean is calculated by taking average of the sum by using a for loop . after that we need to calculate variance using the formula “value=a[i]-mean; sumsqr=sumsqr + value*value;” and var = sumsqr/ n; then now we need to calculate the standard deviation of the set, we use SD = sqrt(var) formula in this c program.
Here is source code of the C program to calculate the mean, variance & standard deviation. The C program is successfully compiled and run(on Codeblocks) on a Windows system. The program output is also shown in below.
SOURCE CODE : :
#include<stdio.h> #include<math.h> int main() { int i,n; float std_dev,sum=0,sumsqr=0,mean,value,variance=0.0,a[100]; printf("Enter value of n : "); scanf("%d",&n); printf("\nEnter numbers : "); for(i=0;i<n;i++) { printf("\nNumber %d : ",i+1); scanf("%f",&a[i]); sum=sum+a[i]; } mean=sum/n; sumsqr=0; for(i=0;i<n;i++) { value=a[i]-mean; sumsqr=sumsqr+value*value; } variance=sumsqr/n; std_dev=sqrt(variance); printf("\nMean of %d numbers = %f\n",n,mean); printf("\nVariance of %d numbers = %f\n",n,variance); printf("\nStandard deviation of %d numbers = %f\n",n,std_dev); return 0; }
OUTPUT : :
Enter value of n : 7 Enter numbers : Number 1 : 23 Number 2 : 45 Number 3 : 12 Number 4 : 66 Number 5 : 58 Number 6 : 31 Number 7 : 67 Mean of 7 numbers = 43.142857 Variance of 7 numbers = 405.551025 Standard deviation of 7 numbers = 20.138298
its not right the sd should be 21.751847212199
I know you are saying correct but look at the link below and input these numbers as above and see what results is :
Excelente