问答题 下列给定程序中,函数fun的功能是:根据以下公式求兀值,并作为函数值返回。 例如,当给指定精度的变量eps输入0.0005时,应输出Pi=3.140578。 π/2=1+1/3+1/3×2/5+1/3×2/5×3/7+1/3×2/5×3/7×4/9+…… 请改正程序中的错误,使它能得出正确的结果。 注意:不得增行或删行,也不得更改程序的结构! #include<math.h> #include<stdio.h> double fun(double eps) { double s,t;int n=1;s=0.0; /******found******/ t=0: while(t>eps) { s+=t: t=t*n/(2*n+1); n++: } /******found******/ return(s); } main() { double x: printf("Please enter a precision:"); scanf("%1f",&x); printf("\neps=%1f,Pi=%1f\n",x,fun(x)); }
【正确答案】正确答案: (1)t=1.0; (2)return(s*2);
【答案解析】解析:本题考查:根据给定公式求值,因此需要确定变量定义的数据类型以及如何对其进行初始化;函数返回值。 该题中,我们首先检查变量数据类型前后是否一致,因为变量t定义为double型,所以赋值时要赋以实型数值。return(s)是一个数学错误,应该返回return(s*2)。