使用VC++2010打开考生文件夹下modi1中的解决方案。此解决方案的项目中包含一个源程序文件modi1.c。在此程序中,函数fun的功能是:根据以下公式求π值,并作为函数值返回。 例如,当给指定精度的变量eps输入0.0005时,应输出Pi=3.140

admin2018-10-21  29

问题 使用VC++2010打开考生文件夹下modi1中的解决方案。此解决方案的项目中包含一个源程序文件modi1.c。在此程序中,函数fun的功能是:根据以下公式求π值,并作为函数值返回。
例如,当给指定精度的变量eps输入0.0005时,应输出Pi=3.140578。

请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
试题程序:
#include<conio.h>
#include<stdio.h>
#include<math.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);
}
void main()
{
double x;
printf("\nPlease enter a precision:");
Scanf("%lf",&x);
printf("\nPi=%lf\n",fun(x));
}

选项

答案(1)t=1.0; (2)return(s*2);

解析 该题中,首先检查变量数据类型前后是否一致,因为变量t定义为double型,所以赋值时要赋以实型数值。return(s)是一个数学错误,应该返回return(s*2);。
转载请注明原文地址:https://jikaoti.com/ti/Qoa0FFFM
0

最新回复(0)