嘿,所以基本上我有这个问题,我试图把一个方程放在函数里面,但它似乎没有设置函数的值,而是不根本不会改变它。关于函数内部方程的简单问题
这是一个捕食猎物模拟,我有这个代码在一个for循环。
wolves[i+1] = ((1 - wBr) * wolves[i] + I * S * rabbits[i] * wolves[i]);
rabbits[i+1] = (1 + rBr) * rabbits[i] - I * rabbits[i] * wolves[i];
当我执行这一点,它按预期工作和但是适当地改变这两个阵列的价值,当我试图把它在函数中,
int calcRabbits(int R, int rBr, int I, int W)
{
int x = (1 + rBr) * R - I * R * W;
return x;
}
int calcWolves(int wBr, int W, int I, int S, int R)
{
int x = ((1 - wBr) * W + I * S * R * R);
return x;
}
,并设置值因此
rabbits[i+1] = calcRabbits (rabbits[i], rBr, I, wolves[i]);
wolves[i+1] = calcWolves(wBr, wolves[i], I, S, rabbits[i]);
这些值与它们初始化时的值保持不变,它似乎根本不起作用,我不知道为什么。我一直在这里待了好几个小时,这可能是我错过的东西,但我无法弄清楚。
任何和所有的帮助表示赞赏。
编辑:我意识到参数是错误的,但我以前用正确的参数尝试过,它仍然没有工作,只是不小心将其更改为错误的参数(编译器鼠标悬停显示旧版本的参数)
EDIT2:整个代码段是这
days = getDays(); // Runs function to get Number of days to run the simulation for
dayCycle = getCycle(); // Runs the function get Cycle to get the # of days to mod by
int wolves[days]; // Creates array wolves[] the size of the amount of days
int rabbits[days]; // Creates array rabbits [] the size of the amount of days
wolves[0] = W; // Sets the value of the starting number of wolves
rabbits[0] = R; // sets starting value of rabbits
for(int i = 0; i < days; i++) // For loop runs the simulation for the number of days
{
// rabbits[i+1] = calcRabbits (rabbits[i], rBr, I, wolves[i]);
// // //This is the code to change the value of both of these using the function
// wolves[i+1] = calcWolves(wBr, wolves[i], I, S, rabbits[i]);
// This is the code that works and correctly sets the value for wolves[i+1]
wolves[i+1] = calcWolves(wBr, wolves[i], I, S, rabbits[i]);
rabbits[i+1] = (1 + rBr) * rabbits[i] - I * rabbits[i] * wolves[i];
}
编辑:我意识到我的错误,我把RBR和WBR在为int,而且他们这是该低于1号花车,所以他们被自动转换为0.谢谢sje
看不出有什么问题。至少显示整个循环?你可以使用'return((1-wBr)* W + I * S * R * R);'.etc – tcooc 2010-06-24 03:02:43
calcWolves()的参数顺序看起来不正确。 – 2010-06-24 03:04:49
我原本设置为只返回那个,但是当那个不起作用时,我添加了x来查看是否有任何可以修复它的原因,不用说它没有。 – 2010-06-24 03:04:49