问题 填空题

在主函数中从键盘输入若干个数放入数组中,用0结束输入并放在最后一个元素中。下列给定程序中,函数proc()的功能是计算数组元素中值为负数的平均值(不包括0)。
例如,数组中元素的值为78、-65、-15、35、-45、0,则程序的运行结果为-41.666667。
请修改程序中的错误,使它能得到正确结果。
注意:不要改动main()函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include<stdio.h>
#inelude<conio.h>
#include<stdio.h>
double proc(int x[])

double sum=0.0;
int e=0, i=0;
//************found*************
while(x[i]==0)

if(x[i]<0)

sum=sum+x[i];
c++;

i++;

//************found*************
sum=sum\c;
return sum;

void main()

int x[1000];
int i=0;
system("CLS");
printf("\nPlease enter some data(end with 0): ");
do

scanf("%d", &x[i]);

while(x[i++]!=0);
printf("%f\n", proc(x));

答案

参考答案:(1)错误:while(x[i]==0) 正确:while(x[i]!=0)
(2)错误:sum=sum\c; 正确:sum=sum/c;

解析: 从题目中可知,数组是以0作为结束符。当数组元素为非0时,需要继续操作后面的元素,因此while(x[i]==0)应改为while(x[i]!=0);在C语言中,数学运算符除号用/来表示,因此“sum=sum\c; ”应改为“sum=sum/c;”。

单项选择题
多项选择题