Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that takes as input five numbers and outputs the mean (average)

ID: 3928859 • Letter: W

Question

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).

Explanation / Answer

#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

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote