问答题
1. m个人的成绩存放在score数组中,请编写函数proc(),它的功能是:将高于平均分的人数作为函数值返回,将高于平均分的分数放在up所指的数组中。
例如,当score数组中的数据为100,80,76,60,59,48,43,35,59时,函数返回的人数应该是4,up中的数据应为100,80,76,95。
注意:部分源程序如下。
请勿改动main()函数和其他函数中的任何内容,仅在函数proc()的花括号中填入所编写的若干语句。
试题程序: #include<stdlib.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
int proc(int score[],int m, int up[])
{
//返回高于平均分的人数
}
void main()
{
int i,n,up[9];
int score[9]={100,80,76,60,59,
48,43,35,95};
system("CLS");
n=proc(score,9,up);
printf("\n up to the average score are:%d\n",n);
for(i=0;i<n;i++)
printf("%d",up[i]);
}
【正确答案】int proc(int score[],int m, int up[])
{
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)//如果分数高于平均分,则将此
分数放入数组up中
up[j++]=score[i];
return j;//返回高于平均分的人数
}
【答案解析】 要找出低于平均分数的学生的记录,首先,应该算出所有学生的平均成绩;然后,将每一个学生的成绩与平均成绩比较,将低于平均成绩的学生的记录放入数组below中;最后,将低于平均分的人数返回给主函数。