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