填空题 已知一个数列,从0项开始的前3项为0、0、1,以后的各项都是其相邻的前3项之和。下列给定的程序中,函数proc()的功能是:计算并输出该数列前n项的平方根之和sum。n的值通过形参传入。例如,当n=11时,程序的输出结果应为32.197745。请修改程序中的错误,使它能得出正确的结果。
注意:不要改动main()函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
//****found****
proc(int n)
{
double sum,s0,s1,s2,s;int k;
sum=1.0;
if(n<=2)sum=0.0;
s0=0.0;s1=0.0;s2=1.0;
for(k=4;k<=n;k++)
{
s=s0+s1+s2;
sum+=sqrt(s);
s0=s1;s1=s2;s2=s;
}
//****found****
return sum
}
void main()
{
int n;
system("CLS");
printf("Input N="):
scanf("%d",&n);
printf("%f/n",proc(n));
}
【正确答案】
【答案解析】错误:proc(int n)
正确:double proc(int n)
错误:return sum
正确:return sum; [解析] 从主函数中的函数调用可知,函数proc()有double型的返回值,因此“proc(int n)”应改为“double proc(int n)”;在C语言中,每一个语句都要以分号结束,因此应在语句“return sum”后加上分号。