Standard Deviation using Function


#include<stdio.h>
#include<conio.h>
#include<math.h>
float mean(int a[],int n);
float std(int a[],int n,float m);
void main()
{
float m,sd;
int n,a[10],i;
clrscr();
printf(“\nenter the number of values\n”);
scanf(%d”,&n);
printf(“\n enter the elements\n”);
for(i=0;i<n;i++)
scanf(%d”,&n);
m=mean(a,n);
printf(“mean=%f\n”,m);
sd=std(a,n,m);
printf(“\n sd=%f”,sd);
getch();
}
flaot mean (inta[],intn)
{
float f;
int sum=0;
for(i=0;i<n;i++)
sum=sum+ a[i];
f=(float)sum/n;
return f;
}
float std(int a[],int n,float m)
{
int i;
float std,sum=0.0,d;
for(i=0;i<n;i++)
{
d=a[i]-m;
a=d*d;
sum=sum+d;
}
sd=sqrt(sum\n);
return sd;
}

Output:
enter the number of values
5
enter the elements
2
4
6
8
10
mean=6.000000
sd=2.828427 

Labels