I need help with this code, for some reason, what I have wont really work out. I
ID: 3873619 • Letter: I
Question
I need help with this code, for some reason, what I have wont really work out. I believe I have made the code correct, but I think I may be off from what we are supposed to use. Please use C and help me understand with comments. Thanks!
1. This program below performs some basic statistics on an array of integers. It must use the five functions indicated. You need to complete this program (write the five functions). If you are using the math fun ctions sqrt and pow, your compiler t require the compile option -Im, as in gcc -Wall -1m stats.c #include #include void getData (int, int[]); double calcMean (int, int[i) double calcVariance (int, int[]double); double calcStdDev (double) void printResults (double, double, double) int main (void) i int size; double mean, variance, stddev; printf ("Enter the array size : "); scanf ("%d" , &size;) ; int array[size]; getData (size, array); mean - calcMean (size, array); variance = calcvariance(size , array , mean); stddev - calcStdDev (variance); printResults (mean, variance, stddev); return 0; Hint: the math involved is not hard and can be found at https://www.mathsisfun.com/data/standard-deviation.htmlExplanation / Answer
#include<stdio.h>
#include<math.h>
void getData(int size, int a[]){
int i;
printf("Enter %d numbers: ",size);
for ( i = 0; i < size; i++){
scanf("%d", &a[i]);
}
}
double calcMean(int size, int a[]){
int i;
double sum = 0;
double mean;
for (i=0; i<size; i++){
sum = sum + a[i];
}
mean = sum/size;
return mean;
}
double calcVariance(int size, int a[], double m){
int i;
double sum = 0;
double variance;
for (i=0; i<size; i++){
sum = sum + abs(a[i] - (int)m) * abs(a[i] - (int)m);
}
variance = sum/size;
return variance;
}
double calcStdDev(double v){
return sqrt(v);
}
void printResults(double a, double b, double c){
printf(" Mean is %lf ",a);
printf(" Variance is %lf ",b);
printf(" Standard Deviation is %lf ",c);
}
int main(){
int size;
double mean,variance,stddev;
printf("Enter the array size:");
scanf("%d",&size);
int array[size];
getData(size,array);
mean = calcMean(size,array);
variance = calcVariance(size,array,mean);
stddev = calcStdDev(variance);
printResults(mean,variance,stddev);
return 0;
}