gpt4 book ai didi

c - 辛普森法则积分

转载 作者:行者123 更新时间:2023-11-30 18:54:52 26 4
gpt4 key购买 nike

我的作业之一是创建一个使用辛普森 1/3 规则求和的 C 程序。我遇到了无法解决的问题。有更多经验的人可以给我指出正确的方向吗?

理论上,我的代码集成了 y=ax^2+bx+c,其中用户选择 a、b、c 的值,然后用户选择上限和下限 [d,e]。然后用户选择将区域分割成更多矩形的 n 值(我们将在类里面使用的值是 100,因此该区域被分割成 100 个矩形)。之后它会运行辛普森规则并打印出总和。

//n is required number of iterations.

#include<stdio.h>
#include<conio.h>
#include<math.h>

double integral (int a,int b,int c,int d,int e,int n)

int main()
{
double a, b, c, d, e, n;

printf("Please select values for y=ax^2+bx+c");
printf("Please select value for a");
scanf("%d", &a);
printf("Please select value for b");
scanf("%d", &b);
printf("Please select value for c");
scanf("%d", &c);
printf("Please select value for the upper limit");
scanf("%d", &d);
printf("Please select value for the lower limit");
scanf("%d", &e);
printf("Please select the number of rectangles for the Simpson's Rule (Input 100)");
scanf("%n", &n);

int i;
double sum=0,length=(double)(d-e)/(n),ad,bd,cd,dd;

ad=(double)a;
bd=(double)b;
cd=(double)c;
dd=(double)d;
for (i=0;i<n;i++)
{
sum+=(ad*(dd*dd+2*dd*length*i+length*length*i*i)+bd*(dd+length*i)+cd)*length;
printf("the value is = %d",sum);
}
return sum;
}

最佳答案

你为什么这么认为

scanf("%e", &e);

应该是这样吗?

scanf()函数采用格式说明符来匹配扫描的输入,在您的情况下,您希望将值存储在 double 变量中,为此您需要 "%lf" 说明符,因此所有 scanf() 都应更改为

scanf("%lf", &whateverDoubleVariableYouWantToStoreTheResultIn);

您不需要从给定类型的变量转换为相同类型,如下所示

dd=(double)d;

而且,您必须知道 scanf() 返回一个值,您不应该忽略它,因为如果输入错误,您的程序将出现错误,您应该检查 scanf() 在库手册或 C 标准中,以更好地了解如何使用它。

关于c - 辛普森法则积分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29400093/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com