Write a program that takes as input five numbers and outputs

Write a program that takes as input five numbers and outputs the mean (average) and standard deviation of the numbers. If the numbers are X_1, X_2, X_3, X_4, X_5 then the mean is equal to X = (X_1 + X_2 + X_3 + X_4 + X_5)/5 and the standard deviation is: squareroot (X1 - X)^2 + (X2 - X)^2 + (X3 - X)^2 + (X4 - X)^2 + (X5 - X)^2/5 Your program must contain at least the following functions: a function that calculates and returns the mean and a function that calculates the standard deviation. Turn-in a print out of your source code and upload your source code to Sakai (lab_4 folder).

Solution

#include <stdio.h>
#include <math.h>
float mean(int a[])
{
int sum = 0,i;
for(i=0;i<5;i++)
{
sum+=a[i];
}
return sum/5;
}

float sdev(int a[])
{
float m = 0,sd=0;
int i;
for(i=0;i<5;i++)
{
m+=a[i];
}
m = m/5.0;
for(i=0;i<5;i++)
{
sd = sd + (a[i]-m)*(a[i]-m);
}
sd = sd/5.0;
sd = sqrt(sd);
return sd;
}

int main()
{
int arr[5];
int i;
  
printf(\"Enter 5 numbers separating with spaces : \");
for(i=0;i<5;i++)
{
scanf(\"%d\",&arr[i]);
}
  
printf(\"Mean : %f\",mean(arr));
printf(\"Standard Deviation : %f\",sdev(arr));
}

/*

---------------------------------------------------------------------
SAMPLE OUTPUT

Enter 5 numbers separating with spaces : 1 2 3 4 5   
Mean : 3.0000000   
Standard Deviation : 1.414214

*/

 Write a program that takes as input five numbers and outputs the mean (average) and standard deviation of the numbers. If the numbers are X_1, X_2, X_3, X_4, X
 Write a program that takes as input five numbers and outputs the mean (average) and standard deviation of the numbers. If the numbers are X_1, X_2, X_3, X_4, X

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site