填空题
以下程序求方程的一个近似根。root函数采用二分法计算并返回方程f(x)=0在[a,b]内的一个近似根,main函数调用root函数求方程cos(x)=0在[0,3.14]内的一个近似根。
试完善程序以达到要求的功能。
#include
#include
double root(double a,double b,double(*f)(double))
{double x,y;
if( 【19】 )
{printf("There is no root between%f and%f",a,b);
return 0;
}
do
{ x= 【20】 ;
y=f(x);
if(fabs(y)<1e-6||fabs(b-a)1
{printf("/n x=%f",root(0,3.14, 【22】 );
【正确答案】
1、(19) f(a)*f(b)>0
(20) (a+b)/2
(21) y*f(a)或f(x)*f(a)
(22) cos
【答案解析】