填空题
1. 下列给定程序中,请补充函数proc(),该函数的功能是:输出数组元素中小于平均值的元素。在主函数main()中,从键盘输入若干个数放入数组str中,并以0结束输入,但0不计入数组。
例如,数组中元素的值依次为2,3,4,5,5,则程序的运行结果为2,3。
注意:部分源程序如下。
请勿改动main()函数和其他函数中的任何内容,仅在函数proc()的横线上填入所编写的若干表达式或语句。
试题程序:
衔nclude<stdlib.h>
#include<conio.h>
#include<stdio.h>
void proc(______,int n)
{
double sum=0.0;
double average=0.0;
int i=0;
for(i=0; i<n; i++)
______;
average=______;
for(i=0; i<n; i++)
if(x[i]<average)
{
if(i%5==0)
printf("\n");
printf("%d,",x[i]);
}
}
void main()
{
int str[1000];
int i=0;
system("CLS");
printf("\nPlease enter some data(end with 0):");
do
{
scanf("%d",&str[i]);
}
while(str[i++]!=0);
proc(str,i-1);
}
【正确答案】
1、int x[]
sum+=x[i]
sum/n
【答案解析】 根据main()函数中实参的类型可知,空一处填“int x[]”;要算出所有元素的平均值,可以先算出所有元素的总和,然后再算出平均值,因此,空二处填“sum+=x[i]”;空三处填“sum/n”。