主题:急,求助,大虾们帮忙!!
以下代码是求1/x从1到10的积分值,但看不出哪出问题了,运行不出来,请大虾们帮忙解决!!
#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);
}
#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);
}