C Program to Calculate Standard Deviation

Standard Deviation (SD) is a quantity expressing by how much the members of a group differ from the mean value for the group. Its formula is 

#include <stdio.h>
#include <conio.h>
#include <stdlib.h> /*for DMA*/
#include <math.h>

void main()
{
    int n,i;
    float *x,sum=0,mean,add_deviation=0;
    printf("Enter how many records are there?: ");
    scanf("%d",&n);
    x=(float*)calloc(n,sizeof(float));
    if(x==NULL)
        printf("Hey! Memory not allocated.");
    printf("Enter data:\n");
    for(i=0;i<n;i++)
    {
        scanf("%f",x+i);
        sum+=*(x+i);
    }
    mean=sum/n;
    printf("\nMean = %f\n",mean);
    for(i=0;i<n;i++)
        add_deviation+=(*(x+i)-mean)*(*(x+i)-mean);
    printf("\nStandard Deviation (SD) = %f",sqrt(add_deviation/n));
    free(x);
    getch();
}


OUTPUT:


No comments:

Post a Comment