In: Math
Develop a C-code to calculate average, median and standard deviation, using 5 numbers as data you input from the keyboard from calculations and compare program output with the hand calculations shown with formula for average, median & standard deviation..
Use suggested approach below for developing the code (other approaches are also possible):
In main define an array of size = 5,
Read in 5 decimal numbers and print the chosen numbers for input; Store input as a array and work with the array only;
define a pointer pointing to the beginning of an array,
pass the array using pointer to function to function_average to compute ‘average’ that is passed back to main;
pass the array using pointer to function to function_median to compute ‘median’ that is passed back to main;
pass the array using pointer to function_standard_derivation to compute ‘standard deviation’ that is pass bac to main;
print results of the computed ‘average’, ‘median’ and ‘standard deviation’ with words of
‘Average of the input five numbers are’, ‘Median of the input five numbers is’ and ‘Standard Deviation of the input five numbers is”
Create function_average
Create function_median,
Create function_standard_derivation
the code is given below
#include<stdio.h>
#include<math.h>
int main() {
double a,b,c,d,e; // Define 5 variables
double avg ,mean,sd; // define average , Mean, standard deviation
printf("Enter five numbers: \n");
scanf("%lf",&a);
scanf("%lf",&b);
scanf("%lf",&c);
scanf("%lf",&d);
scanf("%lf",&e);
int a1,b1,c1,d1,e1;
a1 = (int)a;
b1 = (int)b;
c1 = (int)c;
d1 = (int)d;
e1 = (int)e;
printf("\n\nData: %d %d %d %d %d\n",a1,b1,c1,d1,e1);
avg = (a+b+c+d+e)/ 5.0;
mean = (a + b + c + d + e) / 5.0;
sd = sqrt(((pow(a-mean,2)) + (pow(b-mean,2)) + (pow(c-mean,2)) + (pow(d-mean,2)) + (pow(e-mean,2)))/ 5.0);
printf("Results: \n Average = %6.4f\n Mean = %6.4f\n Standard Deviation = %6.4f\n",avg,mean,sd);
return 0;
}
Sample output
Enter five numbers:
42
44
142
144
143
Data: 42 44 142 144 143
Results:
Average = 103.0000
Mean = 103.0000
Standard Deviation = 48.9980
if it helps to you, hit a thumbs up.
if you have any doubts, comment accordingly