主题:请教
这是我想练习用指针变量作函数参数的例子,结果运行没有错误.可系统提示floating point error(Domain).请各位大侠给以指点.问题好像出在29行的.先谢了
#include "math.h"
main()
{float f1(float x),f2(float x);
float a,b,c,(*P)();
int n=5;
float (*p)(float);
float integral(float a,float b,float (*p)(float),int n);
printf("input a,b:");
scanf("%f%f",&a,&b);
p=f1;
c=integral(a,b,p,n);
printf("The first integral is %6.2f\n ",c);
p=f2;
c=integral(a,b,p,n);
printf("The second integral is %6.2f \n",c);
}
float integral(float a,float b,float (*P)(),int n) /* method for integral*/
{float h,x,sum,(*p)();
int i;
h=(b-a)/n;
x=a;
sum=0;
for(i=1;i<=n;i++)
{x=x+h;
sum=(*p)(x)*h+sum;
}
return(sum);
}
float f1(float x)
{float z=1+x;
return(z);
}
float f2(float x)
{float z=2*x+3;
return(z);
}
#include "math.h"
main()
{float f1(float x),f2(float x);
float a,b,c,(*P)();
int n=5;
float (*p)(float);
float integral(float a,float b,float (*p)(float),int n);
printf("input a,b:");
scanf("%f%f",&a,&b);
p=f1;
c=integral(a,b,p,n);
printf("The first integral is %6.2f\n ",c);
p=f2;
c=integral(a,b,p,n);
printf("The second integral is %6.2f \n",c);
}
float integral(float a,float b,float (*P)(),int n) /* method for integral*/
{float h,x,sum,(*p)();
int i;
h=(b-a)/n;
x=a;
sum=0;
for(i=1;i<=n;i++)
{x=x+h;
sum=(*p)(x)*h+sum;
}
return(sum);
}
float f1(float x)
{float z=1+x;
return(z);
}
float f2(float x)
{float z=2*x+3;
return(z);
}