Hello,
I recently created a program that basically gets all the test scores from an input file and prints out the average of those scores.
Now I am trying to add on to that program by creating a function that figures out the standard deviation and prints it out on the screen.
I was successfully able to do this. The only problem I ran into is printing out the exact value of the standard deviation rounded to two decimal places. Right now, if the standard deviation is 6.48 its going to print out 6.00. If my standard deviation is 14.22 its going to print out 14.00....
Can someone please help??
Code:
#include <stdio.h>
#include <math.h>
int Average(FILE *ifp, int num);
float Standard(FILE *ifp, float num2, float themean);
int main () {
FILE *ifp;
ifp = fopen("test.txt", "r");
int numScores, mean;
fscanf(ifp, "%d", &numScores);
printf("Your average for all the tests you took is %d.\n", Average(ifp, numScores));
mean = Average(ifp, numScores);
printf("The standard deviation is %.2f.\n", Standard(ifp, numScores, mean));
getch();
return 0;
}
int Average(FILE *ifp, int num){
int i,z, average, sum=0;
int testScores [200];
for(i=0; i < num; i++){
fscanf(ifp, "%d", &testScores[i]);
}
for(z=0; z < num; z++) {
sum=sum+testScores[z];
}
average = sum /num;
return average;
}
float Standard(FILE *ifp, float num2, float themean){
int i=0, sum=0, z=0, result;
int MyScores[200];
for(i=0; i < num2; i++) {
fscanf(ifp, "%f", &MyScores[i]);
MyScores[i]=MyScores[i]-themean;
if(MyScores[i] < 0)
MyScores[i]=MyScores[i]*(-1);
MyScores[i]=MyScores[i]*MyScores[i];
}
for(z=0; z < num2; z++){
sum = sum + MyScores[z];
}
result = sqrt(sum/(num2-1));
return result;
}
My Txt doc.
10
88
65
72
92
100
57
70
88
90
65