主题:复化梯形积分法
#include<stdio.h>
float f1(float x)
{ float y;
y=1/x;
return y;
}
float f2(float(*f1)(float),float a,float b,int n)
{ float s,s1,s2,h;
int k;
s=0;
h=(b-a)/n;
for(k=0;k<=n-1;k++)
{
s1=(*f1)(1/(a+k*h));
s2=(*f1)(1/(a+k*h+h));
s=s+(s1+s2)*h/2;
}
return s;
}
void main()
{ int n;
float a,b,sum;
a=1;
b=10;
scanf("%d",&n);
sum=f2(f1,a,b,n);
printf("%f",sum);
}
看不出哪出问题了,运行不出来,请大虾们帮忙解决!!
float f1(float x)
{ float y;
y=1/x;
return y;
}
float f2(float(*f1)(float),float a,float b,int n)
{ float s,s1,s2,h;
int k;
s=0;
h=(b-a)/n;
for(k=0;k<=n-1;k++)
{
s1=(*f1)(1/(a+k*h));
s2=(*f1)(1/(a+k*h+h));
s=s+(s1+s2)*h/2;
}
return s;
}
void main()
{ int n;
float a,b,sum;
a=1;
b=10;
scanf("%d",&n);
sum=f2(f1,a,b,n);
printf("%f",sum);
}
看不出哪出问题了,运行不出来,请大虾们帮忙解决!!