例8.11 有一个一维数组score,内放10个学生成绩,求平均成绩。
程序如下:
float average(float array[10])
{int i;
float aver,sum=array[0];
for(i=1;i<10;i++)
sum=sum+array[i];
aver=sum/10;
return(aver);
}
main()
{float score[10],aver;
int i;
printf("input 10 scores:\n");
for(i=0;i<10;i++)
scanf("%f",&score[i]);
printf("\n");
aver=average(score);
printf("average score is %5.2f",aver);
}
运行情况如下:
input 10 scores:
100567898.576879967.57597
average score is 83.40
程序如下:
float average(float array[10])
{int i;
float aver,sum=array[0];
for(i=1;i<10;i++)
sum=sum+array[i];
aver=sum/10;
return(aver);
}
main()
{float score[10],aver;
int i;
printf("input 10 scores:\n");
for(i=0;i<10;i++)
scanf("%f",&score[i]);
printf("\n");
aver=average(score);
printf("average score is %5.2f",aver);
}
运行情况如下:
input 10 scores:
100567898.576879967.57597
average score is 83.40