问答题
1. n个人的成绩存放在score数组中,请编写函数proc(),它的功能是:将低于平均分的人数作为函数值返回,将低于平均分的分数放在down所指的数组中。
例如,当score数组中的数据为{99,80,40,56,59,84,60,76,100}时,函数返回的人数应该是4,down中的数据应为{40,56,59,60}。
注意:部分源程序如下。
请勿改动main()函数和其他函数中的任何内容,仅在函数proc()的花括号中填入所编写的若干语句。
试题程序:
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
int proc(int score[],int n,int down[])
{
}
void main()
{
int i,n,down[9];
int score[9]={99,80,40,56,59,84,60,76,100};
system("CLS");
n=proc(score,9,down);
printf("\ndown to the average score are:");
for(i=0; i<n; i++)
printf("%d",down[i]);
printf("\n");
}
【正确答案】int proc(int score[],int n,int down[])
{
int i,j=0;
float av=0.0;
for(i=0; i<n; i++)
av=av+score[i]/n; //求其平均数av
for(i=0; i<n; i++)
if(score[i]<av)//通过if语句来把小于平均数的数放到down数组中
down[j++]=score[i];
return j;
}
【答案解析】 要找到所有学生中成绩低于平均分数的人数,首先需要算出所有学生的平均成绩,然后将所有学生的成绩与平均成绩比较,将低于平均分数学生的记录存放在新的数组中,并将低于平均分数的学生数返回给主函数。