Dates:
For all the homework assignments, including this one, you may submit to Dora and then check whether you passed her tests. If you did not pass the tests, then you should fix the problem and resubmit. You may resubmit as often as needed before the deadline. (However, for the lab exams, you may submit only once.) |
Homework Policy
All the work for Part 6A must be entirely your own.
You may consult books and the course instructors, but nothing else.
You may work with other students on Part 6B.
At the top of your program, put the names of any others that
you have worked with. You must completely understand all
work that you submit.
"On my honor as a University of Colorado at Boulder student, I have neither received nor given unauthorized assistance on this work."
Due at 9:30am, Tuesday, Oct 8. No late submissions accepted for credit.
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 = (1 - d + bG)F New goose population = (1 + r - rG/k - aF)GThe variables in these equations are:
F = last year's fox population (try about 100) G = last year's goose population (try about 10000) d = death rate of foxes (try about 0.1) b = conversion rate (try about 0.00001) r = growth rate of geese (try about 0.4) a = ability rate (try about 0.0005) k = carrying capacity of island (try about 30000)
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 a function to compute the fox and goose populations over a period of 100 years. The constants and prototype for the function must be exactly these (so that Dora can check your work):
const int INITIAL_FOX = 100; const int INITIAL_GEESE = 10000; 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 int CAPACITY = 30000; const int YEARS= 100; void population(int foxes[], int geese[]); // This function uses the declared constants to compute the fox and // goose population for future years, putting these values in // foxes[0]...foxes[YEARS] and geese[0]...geese[YEARS]. // THIS FUNCTION DOES NOT WRITE ANY OUTPUT TO cout. // IT JUST PUTS THE NUMBERS IN THE ARRAY.
Notice that the
size of the two arrays must be at least YEARS+1
since the
function will compute values for foxes[0]...foxes[YEARS]
and geese[0]...geese[YEARS]
.
For Part 6A of this assignment (due at 9:30 Oct 8 with no late submissions), your program must produce an output of exactkt this form:
---------- 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 minimum population for foxes was 100 in year 0. The maximum population for foxes was 622 in year 26. The minimum population for geese was 8338 in year 34. The maximum population for geese was 23404 in year 9.The program must work correctly even if Dora 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;In addition to the items listed above, this version of the program is required to have the following items for Dora to test:
const int YEARS= 100; // Number of years in the simulation const int TABLE_INCREMENT = 10; // Size of the steps in the output table void computeMinimum(int data[], int& minValue, int& i); // The function examines data[0]...data[YEARS] to find the minimum entry. // The value of this entry is placed in the parameter minValue. // The index of this entry is placed in the parameter i. // If there are several equally small minimum elements, then i is set // to the index of the first such item. // THIS FUNCTION DOES NOT WRITE ANY OUTPUT TO cout. // IT JUST PUTS THE NUMBERS IN THE TWO REFERENCE PARAMETERS. void computeMaximum(int data[], int& maxValue, int& i); // The function examines data[0]...data[YEARS] to find the maximum entry. // The value of this entry is placed in the parameter maxValue. // The index of this entry is placed in the parameter i. // If there are several equally large maximum elements, then i is set // to the index of the first such item. // THIS FUNCTION DOES NOT WRITE ANY OUTPUT TO cout. // IT JUST PUTS THE NUMBERS IN THE TWO REFERENCE PARAMETERS.You should write and propose other functions as needed to satisfy your sense of top-down design and the required class style guide.
Hint: The formulas produce a double number for each year's population. You should store this value in a double number and then convert it to an integer before assigning it to an element of the integer array. For example, if p is a double number, then you may write the assignment:
foxes[i] = (int)p;
Due at 9:30am, Tuesday, Oct 15. No late submissions accepted for credit.
Modify your program so that it also draws a graph of these populations, using the x-axis for time and the y-axis for populations. There should be two different lines on the graph for the fox and geese populations. The two lines should be drawn in different colors and using different scales so that the maximum populations are near the top of the screen in both cases. Label the x axis every 20 years; put two different labels on the y-axis to show the two different scales. The y-axis scales must have at least three numbers each (one at the top, one at the bottom and one in the middle). There are notes at www.cs.colorado.edu/~main/intro/info/outtext.html that talk about printing labels on axes in graphics mode.