(C++) CPP Programs - 16
Friday, December 14, 2007
16. Write a C program to input n real numbers and to find Mean, Variance and Standard deviation,
where Mean=( x[i] ) / n, Variance = ∑ ( x[i] – Mean ) 2 /n, Standard deviation = √(Variance) for 1<=i<=n. Output the computed results on different lines.
/*Program to find mean,std_dev,variance*/
#include
#include
#include
void main()
{
float x[45],nr=0.0,std_dev,var,mean,sum=0.0;
int n,i;
clrscr();
printf("Enter how many elements?\n\a");
scanf("%d",&n);
printf("Enter the numbers one by one\n\a");
for(i=0;i <>
scanf("%f",&x[i]);
for(i=0;i <>
sum+=x[i];
mean=sum/n;
for(i=0;i <>
nr+=pow((x[i]-mean),2);
var=nr/n;
std_dev=sqrt(var);
printf("Mean of all elements=%f\n",mean);
printf("Standard deviation of elements=%f\n",std_dev);
printf("Variance=%f\n",var);
getch();
}
0 comments:
Post a Comment