问题描述
- 结果应该是80.40和78.20,请问是哪里出错了,菜鸟求大神指教
-
#include
int main()
{
float average(float array[], int n);
float score1[5] = { 98.5,97,91.5,60,55 };
float score2[10] = { 67.5,89.5,99,69.5,77,89.5,76.5,54,60,99.5 };
printf("the average of class A is %6.2fn", average(score1, 5));
printf("the average of class B is %6.2fn", average(score2, 10));
}
float average(float array[], int n)
{
int i;
float aver, sum = array[0];
for (i = 1; i < n; i++);
sum = sum + array[i];
aver = sum / n;
return(aver);
}
解决方案
多了一个分号,去掉即可:
for (i = 1; i < n; i++);
解决方案二:
#include<stdio.h>
int main()
{
float average(float array[], int n);
float score1[5] = { 98.5,97,91.5,60,55 };
float score2[10] = { 67.5,89.5,99,69.5,77,89.5,76.5,54,60,99.5 };
printf("the average of class A is %6.2fn", average(score1, 5));
printf("the average of class B is %6.2fn", average(score2, 10));
}
float average(float array[], int n)
{
int i;
float aver, sum = array[0];
for (i = 1; i < n; i++)
sum = sum + array[i];
aver = sum / n;
return(aver);
}
运行结果:
the average of class A is 80.40
the average of class B is 78.20
请按任意键继续. . .
时间: 2024-09-20 07:23:31