计算方法:复化梯形法
#include <stdio.h>#include <stdio.h>
float fny(float x);
main(void)
{
float a=0.0;
float b=1.0;
int n;
float h,x,t;
printf("请输入n的值:\n");
scanf("%d",&n);
h=(b-a)/n;
x=a;
t=0;
for(int i=1;i<=n-1;i++){
x=x+h; t=t+fny(x);
}
t=(fny(a)+2*t+fny(b))*h/2;
printf("%f\n",t);
}
float fny(float x)
{
float q;
q=1/(1+x*x);
return q;
}
----------------------------------------------------------
#include <stdio.h>
#include <math.h>
float fny(float x);
main(void)
{
float a=0.0;
float b=1.0;
int n;
float h,x,t;
printf("请输入n的值:\n");
scanf("%d",&n);
h=(b-a)/n;
x=a;
t=0;
for(int i=1;i<=n-1;i++){
x=x+h; t=t+fny(x);
}
t=(1+2*t+fny(b))*h/2;
printf("%f\n",t);
}
float fny(float x)
{
float q;
q=sin(x)/x;
return q;
}