Unsure of how to setup this function

This is a discussion on Unsure of how to setup this function within the C Programming forums, part of the General Programming Boards category; I'm so very lost with this formula that is to be used in a function I have to write. I ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    5

    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.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    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.
    Sent from my iPadŽ

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    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)
    Last edited by SKeane; 10-03-2006 at 08:14 AM.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    5
    thank you very much, that was very helpful. Now to finish this thing up

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 07:44 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21