# Unsure of how to setup this function

• 10-03-2006
Khem101
Unsure of how to setup this function
I'm so very lost with this formula that is to be used in a function I have to write. I have to write a function that calculates a number, forgive my slowness but the formula just seems confusing. The function has 4 variables passed to it, from there I need to use the formula to calculate the number, which I then use in another function.

int CalculateRabbits (int numRabbits, int numFoxes, float birthrate,
float interaction)

PRn = PR(n-1) + BR * PR(n-1) - I * PR(n-1)

PRn is the new rabbit population after time period n
PR(n-1) is the beginning rabbit population at the time period n-1
BR is the birth rate for rabbits
PF is fox population, but for the function I'll be using that value will be 0 (rabbit only)

this function gets called in another function that simulates the population growth of rabbits as each year goes by (years is input by the user).

I'd greatly appreciate any help, I'm not looking for the actual code, just help understanding how to go about coding that formula, the time period part is throwing me off, as I'm under the impression the amount of years that will be simulated has nothing to do with this function, but instead the one that will be used after this one.
• 10-03-2006
SlyMaelstrom
You don't seem to be telling us, enough. You have all the variables except the time (n). Is that static to the function? Is it a constant? I can't imagine time is ever a constant. Also, you're passing numFoxes, but you already said it's constantly 0, so why pass it at all? It seems to me, and this is just a guess because you haven't really explained it, that PRn will be a sum of rabbit births from t (0 to n)... which means that PRn will be accumulating in a loop as time goes to n.

So my only question is, right now... Where is n? It doesn't seem like you can process that equation without a maximum time period.
• 10-03-2006
Salem
Do a couple of iterations of the formula on paper, using nothing more than a calculator.

Use a debugger to step through your code one line at a time, and examine all the intermediate variable values.

When the code isn't the same as your paper exercise, you've found a bug.

Then figure out whether the bug is on your paper, or in your code.

Rinse and repeat until the code works as intended.
• 10-03-2006
SKeane
The CalculateRabbit() function is just doing one round of calculations, it doesn't need a "time" element. Every time it is called is another iteration.

PRP(n-1) + BR * PR(n-1) - I * PR(n-1)

Could more simple be written

PR = CalculateRabbit(PRlast, PFlast, BR, I)

Where

PRlast is either (a) the initial population of rabbits, or (b) the result of the last call to CalculateRabbits().

and

PFlast is either (a) the initial population of foxes, or (b) the result of the last call to CalculateFoxes().

All your CalculateRabbits() function now has to do is return

PRlast + (BR * PRlast) - (I * PRlast * PFlast)
• 10-03-2006
Khem101
thank you very much, that was very helpful. Now to finish this thing up