问题 问答题

m个人的成绩存放在score数组中,请编写函数fun(),它的功能是:将低于平均分的人数作为函数值返回,将低于平均分的分数放在below所指的数组中。 例如,当score数组中的数据为10,20,30,40,50,60, 70,80,90时,函数返回的人数应该是4,below中的数据应为10,20,30,40。 注意:部分源程序给出如下。 请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。 试题程序: #include <conio.h> #include <stdio.h> #include <string.h> int fun(int score[],int m, int below[]) { } main() { iht i, n, below[9]; int score[9]={10,20,30,40,50,60,70, 80,90}; clrscr(); n=fun(score, 9, below); printf("\nBelow the average score are: "); for(i=0;i<n;i++) printf("%d",below[i]); }

答案

参考答案:

解析:int fun(int score[],int m, int below[]) { int i,j=0; float av=0.0; for(i=0;i<m;i++) av=av+score[i]/m; /*求平均值*/ for(i=0;i<m;i++) if(score[i]<av) /*如果分数低于平均分,则将此分数放入below数组中*/ below[j++]=score[i]; return j; /*返回低于平均分的人数*/ } 该题第1个循环的作用是求出平均分av,第2个循环的作用是找出低于平均分的成绩并存入below数组中。

单项选择题
单项选择题