我的平均分錯在哪里?
#include <stdio.h>
int total(int score[])
{
? ? int i,sum;
? ? for(i=0;i<=9;i++)
? ? {
? ? sum += score[i];
? ? }
? ? return sum;
}
float aver(int score[])
{
? ? return total(score);
}
void a(int score[])
{
? ? int i,j;
? ? for(i=8;i>=0;i--)
? ? { ??
? ? ? ? for(j=0;j<=i;j++)
? ? ? ? {
? ? ? ? ? ? if(score[j]>score[j+1])
? ? ? ? ? ? {
? ? ? ? ? ? ? ? int temp;
? ? ? ? ? ? ? ? temp=score[j];
? ? ? ? ? ? ? ? score[j]=score[j+1];
? ? ? ? ? ? ? ? score[j+1]=temp;
? ? ? ? ? ? ? ??
? ? ? ? ? ? }
? ? ? ? }
? ? ? ??
? ? }
? ?
}
int main()
{
? ? int i;
? ? int score[10]={67,98,75,63,82,79,81,91,66,84};
? ? printf("總分%d\n",total(score));
? ? printf("平均%.1f\n",aver(score)); ?
? ??
? ? a(score);
? ? for(i=0;i<=9;i++)
? ? {
? ? printf("%d\n",score[i]);
? ? }
? ?
? ? return 0;
}
2016-12-07
第一個求和函數中sum沒有在開始賦予初值0,你的main函數中求和調用了一次,求平均調用了一次,求平均時的total返回的是每個元素加個兩次的和,導致你求出的平均值是實際平均值78.7的兩倍,那為什么不是157.4呢?因為你aver函數中total(score)返回的是整型值,舍去小數是157,而aver函數是float型,所以返回值被轉換成了浮點數157.0.
2016-12-07
你沒有設置總個數,平均數函數中也沒有除以總個數
2016-12-07
你只是返回total了,沒有平均啊。