Sign in to follow this  

c++ loop problem I'm trying to solve.

This topic is 4843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everyone, this is my first time psoting. I need to write a program for school and I'm having some trouble figuring out how to do it. The problem is: "There is an island which is shared by foxes and geese. Each year the populations of these animals varies according to these equations: New fox population = int((1 - d + bG)F) New goose population = int((1 + r - rG/k - aF)G) The numbers F and G in the equations are the previous year's fox and goose populations. The other numbers in the equations are these constants: d = death rate of foxes (0.1) b = conversion rate (0.00001) r = growth rate of geese (0.4) a = ability rate (0.0005) k = carrying capacity of island (30000.0) These fancy phrases probably don't mean much to you, but the equations should be enough to program the simulation. If you want to find out more about this mathematical model, see the book Population Biology by P.W. Hendrick. This program declares a group of constants and then uses two formulas to compute the fox and goose populations over a period of 100 years. The constants and prototype for the functions must be exactly these (so that your TA can check your work): const double DEATH_RATE = 0.1; const double CONVERSION_RATE = 0.00001; const double GROWTH_RATE = 0.4; const double FOX_ABILITY = 0.0005; const double CAPACITY = 30000.0; When the program first starts, it should ask the user for the initial fox and goose populations. The program then calculates predicted fox and goose populations for the next 100 years. As it is calculating the populations, it prints a table that lists the fox and goose populations every ten years. (The other nine years are calculated, but not printed.) The first year of the table is year number zero. For example, if the initial populations in year zero were 100 and 10000, then the table would look exactly like this. Dora will reject any other form for the table! ---------- Year ---------- Foxes ---------- Geese 0 100 10000 10 215 23238 20 542 14541 30 607 8693 40 518 9005 50 502 10676 60 535 10476 70 539 9913 80 529 9975 90 527 10168 100 531 10148 The program must continue to work correctly even if your TA changes the values of your constants. Please notice that the lines of dashes in the first line of the output have exactly ten dashes each. The numbers on the subsequent lines must line up as shown in the above example. In order to align such numbers, you will need to use the setw function from #include <iomanip>. This function produces an object that is sent to cout just before printing a number. For example, to print a number x using a total of 15 spaces, you would use the statement: cout << setw(15) << x; Other questions to ask yourself before you start: What variables will you be using? What are the data types of those variables? Suppose that you use a variable called year to keep track of the current year. Write a Boolean expression that will be true only when year is a multiple of 10 (such as 0,10,20,...). Suppose that you keep track of the current fox and goose populations in two variables called foxPop and goosePop. You might try to change these variables to the new populations by just calling your functions, like this: foxPop = newFoxPop(foxPop, goosePop); goosePop = newGoosePop(foxPop, goosePop); This is almost right, but not quite. Do you see why? The problem is that these two statements will first change the foxPop and then use that new value of foxPop when it calls newGoosePop. But that's wrong! When you are calculating the new goose population, you must use the old fox and goose populations. How are you going to solve this problem? If a population drops below zero, then please reset it to zero right away. We don't want negative foxes and geese running around our island!" I've already started and am just trying to figure out how to loop it so that I can print it every year. I realize I need to do year = year+1 every time it loops and when the year is 101 it stops. I'm not sure how to print it on the screen though. Any help would be greatly appreciated.

Share this post


Link to post
Share on other sites

This topic is 4843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.
Sign in to follow this