2010-06-24 25 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