2010-06-24 41 views
2

嘿,所以基本上我有这个问题,我试图把一个方程放在函数里面,但它似乎没有设置函数的值,而是不根本不会改变它。关于函数内部方程的简单问题

这是一个捕食猎物模拟,我有这个代码在一个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

+1

看不出有什么问题。至少显示整个循环?你可以使用'return((1-wBr)* W + I * S * R * R);'.etc – tcooc 2010-06-24 03:02:43

+1

calcWolves()的参数顺序看起来不正确。 – 2010-06-24 03:04:49

+0

我原本设置为只返回那个,但是当那个不起作用时,我添加了x来查看是否有任何可以修复它的原因,不用说它没有。 – 2010-06-24 03:04:49

回答

0

我正在使用一个整数作为double的参数。

0

菲尔我看不出你的代码中有任何明显的错误。

我的预感是你搞乱了参数。

在这一点上使用gdb将是一个过度杀手。我建议你在calcRabbits和calcWolves中打印输出。打印出所有参数,新值和迭代次数。这会给你一个关于正在发生的事情的好主意,并有助于追踪问题。

你有完整的代码初始化,我们可以尝试测试和运行?

+0

意外删除了funcs.cpp中的* /,可能必须将其添加回。 再次感谢 – 2010-06-24 03:36:01

0

我不知道这是问题,但这是

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 

days在运行时确定。这在C++中是非标准的(对于大量的days可能会破坏你的堆栈),你应该只使用数组大小​​的常量。您可以动态调整vector的大小以解决此限制(或堆分配数组)。

改成这样:

std::vector<int> wolves(days); 
std::vector<int> rabbits(days); 

或者这样:

int *wolves = new int[days]; 
int *rabbits = new int[days]; 

// all your code goes here 

delete [] wolves; // when you're done 
delete [] rabbits; // when you're done 

这将动态地分配在堆上的阵列。其余的代码应该是一样的。如果您使用矢量方法,请不要忘记#include <vector>

如果你仍然有问题,我会cout << "Days: " << days << endl;,以确保你从getDays()得到正确的数字。如果你得到零,它似乎表现为“循环不工作”。

+0

谢谢,我会避免使用矢量因为为了课堂的目的,我们还没有与他们合作过。天数工作正常,因为当我尝试运行它与原始方程设置数组的值,它的工作原理,所以我认为我可以孤立的功能作为错误的原因。感谢您的建议,但只要我得到这个运行,我会阅读矢量库。 – 2010-06-24 03:38:48

+0

@菲尔:这仍然是危险的。编辑以显示如何堆分配数组。 – Stephen 2010-06-24 04:04:11