下列给定程序中,函数fun()的功能是:根据以下公式求n的值,并作为函数值返回。例如,给指定精度的变量eps输入 0.0005时,应当输出Pi=3.140578。 n/2=1+1/3+1/3*2/5+1/3*2/5*3/7+1/3*2/5*3/7*4/9…… 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。 试题程序: #include <conio.h> #include <stdio.h> #include <math.h> double fun(double eps) {double s,t; int n=t; s=0.0; /*************found**************/ t=1; /*************found**************/ while(t>eps) { s+=t; t=t*n/(2*n+1); n++; } /*************found**************/ return (s); } main() { double x; printf("\nPlease enter a precision: "); scanf("%1f",&x); printf("\nPi=%1f\n ",fun(x)); }
参考答案:错误:t=0; 正确:t=1.0;
解析:(2) 错误:while(t>eps) 正确:while(t>=eps) (3) 错误:return(s); 正确:return(s*2); 该题中,我们首先看函数fun()中while语句的含义,当新的一项大于给定参数时,循环累加。根据题意我们可以看出,最后一项应该小于给定参数,因此,循环条件应当为while(t>=eps)。至于return(s);错误,是一个数学常识,应该是 return(s*2);。>