此代码是计算函数 f(x) 的积分程序
在这里..函数是 f(x)= x^2
int main()
{
float integral;
float a=0,b=0;
int n=1024;
float h;
float x;
int i;
float f(float x);
printf("Enter a, b \n");
scanf("%f %f" , &a, &b);
printf("Enter n\n");
scanf("%d" , &n);
h=(b-a)/n;
integral = (f(a)+f(b))/2;
x=a;
for (i = 0; i <= n-1; n++)
{
x = x + h;
integral = integral + f(x);
}
integral = integral * h ;
printf("with n = %d trapezoids, our esrimate\n", n );
printf("of the integral from %f to %f = %f\n", a , b , integral);
system("pause");
return 0;
}
float f(float x)
{
return x*x;
}
n 的输出总是垃圾..我不知道为什么我的错在哪里?
这个问题很愚蠢
但我真的很累找错了