#include <stdio.h>
#include <math.h>
double integrateF(double low, double high)
{
double low = 0;
double high = 20;
double delta_x=0;
double x, ans;
double s = 1/2*exp((-x*x)/2);
for(x=low;x<=high;x++)
delta_x = x+delta_x;
ans = delta_x*s;
return ans;
}
它说低和高被"重新宣布为不同类型的符号",我不知道这意味着什么。基本上,我在这里所做的(阅读:尝试)是从低(我设置为 0)到高 (20) 进行积分以找到黎曼和。for 循环看起来也有点迷幻...我太迷茫了。
编辑:
#include <stdio.h>
#include <math.h>
double integrateF(double low, double high)
{
low = 0;
high = 20;
double delta_x=0;
double ans = 0;
double x;
double s = 1/2*exp((-x*x)/2);
for(x=low;x<=high;x++)
{
delta_x = x+delta_x;
ans = ans+(delta_x*s);
}
return ans;
}
^这仍然不起作用,在大括号和所有之后。它说"对'WinMain@16'的未定义引用"......
你在函数内部重新定义了低和高,这与参数中定义的那些冲突。
for 循环正在做
for(x=low;x<=high;x++)
{
delta_x = x+delta_x;
}
你是故意的吗
for(x=low;x<=high;x++)
{
delta_x = x+delta_x;
ans = delta_x*s;
}
但是我认为您想做ans += delta_x*s;
low
和high
已经作为integrateF
方法的参数传递。但是它们在方法中再次重新声明。因此错误。
低和高已经作为 integrateF 方法的参数传递,它们在方法中再次重新声明。
并且 x 在用于计算 s. 时没有被赋值。
双X,ANS;双精度 s = 1/2*exp((-x*x)/2);
你可能想这样尝试:-
for(x=low;x<=high;x++)
{ //Use brackets since you are redefining low and high inside the function
delta_x = x+delta_x;
ans = delta_x*s;
}
或
for(x=low;x<=high;x++)
{ //Use brackets since you are redefining low and high inside the function
delta_x = x+delta_x;
}
编辑:-
它说"对'WinMain@16'的未定义引用"
确保已定义main() or WinMain()
。还要检查 main() 是否未在命名空间中定义
导致此错误的另一种方法是在代码中"重新定义"您的函数,其中该名称标签已用作主函数之外的变量 - 像这样(伪代码):
double integrateF = 0;
main(){
// get vars to integrate ...
}
double integrateF(double, double){
//do integration
}
您甚至不必调用 main 内部的函数来尝试编译时出错,相反,编译器无法理解: double integrateF = 0 = (double, double) { };
在主功能之外。
在参数中声明数据类型后,不必重新声明它们。
而不是
double integrateF(double low, double high)
{
double low = 0;
double high = 20;
.
.
.
}
你应该这样做
double integrateF(double low, double high)
{
low = 0;
high = 20;
.
.
.
}