我的平均分錯在哪里?
#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
第一個求和函數(shù)中sum沒有在開始賦予初值0,你的main函數(shù)中求和調(diào)用了一次,求平均調(diào)用了一次,求平均時的total返回的是每個元素加個兩次的和,導(dǎo)致你求出的平均值是實際平均值78.7的兩倍,那為什么不是157.4呢?因為你aver函數(shù)中total(score)返回的是整型值,舍去小數(shù)是157,而aver函數(shù)是float型,所以返回值被轉(zhuǎn)換成了浮點數(shù)157.0.
2016-12-07
你沒有設(shè)置總個數(shù),平均數(shù)函數(shù)中也沒有除以總個數(shù)
2016-12-07
你只是返回total了,沒有平均啊。