For programming assignments, you may consult with others (instructors, other students, etc.). You may look at and discuss algorithms or programs that are under development provided that you do not make a copy of any code in any way (no file copying, no xerox copies, no copying by hand, etc.), nor may you allow another student to make a copy of your code. You may also use any references (books, web, etc.) provided that include a comment that cites any resources used (not needed for our textbook)
As a student, you are responsible for knowing and adhering to this policy. Violations will be reported to the CU Honor Code Council and result in an F for the entire course.
Please note that this project indicates precisely what your program should accomplish, without a precise indication of how the program works. Part of your assignment is designing the techniques of how the program works. You will need a good understanding of loops to do this.
The purpose of this assignment is to make sure that you know how to write a small program that includes loops, if-statements and two small programmer-defined functions.
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 the two population equations to compute the fox and goose populations over a period of 100 years. The constants must be exactly these (so that we 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;Your program 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 10 years. (The other 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. We 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 10148The program must continue to work correctly even if someone 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;
The required format may seem picky (probably because it is picky), but that is the sort of precise specification that programmers must often follow. Also, it'll make our grading job much easier.
Other questions to ask yourself before you start:
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,...). When this expression is true, you should print
one line of the table. When it is false, you still do all the
calculations, but you do not print anything.
fox_pop
and
goose_pop
. You might try to change these variables
to the new populations by just calling your functions, like this:
fox_pop = formula for new fox population; goose_pop = formula for new goose population;There is a potential pitfall here. The problem is that the first of the two statements will change the
fox_pop
.
Therefore, by the time you get to the second assignment statement,
fox_pop contains the new year's fox population. But the formulat used
in the second assignment requires the previous year's fox population.
How are you going to solve this
problem?
Eventually, you'll need to follow a particular programming style that will be discussed in class. The full guidelines are also available from the link on our class home page. For this program, there are only a few style requirements: