C program - How to Find Variance, Mean and Standard Deviation using C




#include <stdio.h>
#include <conio.h>
#include <math.h>
#define MAXSIZE 10
void main()
{
 float x[MAXSIZE];
 int   i, n;
 float avrg, var, SD, sum=0, sum1=0;
 clrscr();
 printf("Enter the value of N\n");
 scanf("%d", &n);
 printf("Enter %d real numbers\n",n);
 for(i=0; i<n; i++)
 {
  scanf("%f", &x[i]);
 }
 /* Compute the sum of all elements */
 for(i=0; i<n; i++)
 {
  sum = sum + x[i];
 }
 avrg = sum /(float ) n;
 /* Compute  varaience  and standard deviation  */
 for(i=0; i<n; i++)
 {
  sum1 = sum1 + pow((x[i] - avrg),2);
 }
 var = sum1 / (float ) n;
 SD = sqrt(var);
 printf("Average of all elements  = %.2f\n", avrg);
 printf("Varience of all elements = %.2f\n", var);
 printf("Standard deviation       = %.2f\n", SD);
}  /*End of main()*/

Output:

C programs, c programming, c examples,


Post a Comment

 
Top