嘿,所以基本上我有這個問題,我試圖把一個方程放在函數裏面,但它似乎沒有設置函數的值,而是不根本不會改變它。關於函數內部方程的簡單問題
這是一個捕食獵物模擬,我有這個代碼在一個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