我尝试了一切以使标准偏差起作用。我正在尝试制作一个程序来获取学生的成绩(由用户决定计数)并计算成绩的平均值以及标准偏差。它计算平均罚款,但对于标准偏差,它给出“7712160851427328.00”
#include <stdio.h>
int main(){
int i;
int j;
int count;
int grade = 0;
int grades[5] = {0}; //Init
int sum = 0;
float deviation_sum;
float mean;
float standard_deviation;
printf("Give Count: ");
scanf("%d", &count);
for(i = 0; i < count; i++){
printf("Give %d grade: ", (i+1));
scanf("%d", &grade);
switch(grade){
case 0:
grades[0]++;
break;
case 1:
grades[1]++;
break;
case 2:
grades[2]++;
break;
case 3:
grades[3]++;
break;
case 4:
grades[4]++;
break;
case 5:
grades[5]++;
}
sum += grade;
}
mean = sum/count;
printf("mean: %.2f \n", mean);
for(i = 0; i <= 5; i++){
while(grades[i] == 0){
i++;
}
for(j = 0; j < grades[i]; j++){
deviation_sum += (i-mean)*(i-mean);
printf("%d,%d\n",i,j);
}
}
standard_deviation = sqrt(deviation_sum /count - 1);
printf("deviation: %.2f\n", standard_deviation);
}
我认为问题出在最后一个 for 循环中,只是无法弄清楚。