CMSC201
Programming Project One
Predator/Prey Simulation
Out: Tuesday 9/26/06
Due: Tuesday 10/3/06,
Before Midnight
|
The Objective
The objective of this assignment is to get you started writing programs
in C in the UNIX environment. Its main emphasis is design, so we will be
designing the project together in class. This project will also give
you practice writing functions, loops, switch statements and using
separate compilation.
The Background
Populations of predators and their prey that are isolated tend to achieve an
equilibrium over time. The pattern tends to be a cyclic increase and decrease
in the populations. When the number of predators is low, the number of prey
grow. As the prey increase the predators manage to catch more of them. The
predators are now healthier and can raise their young with fewer dying out, so
the number of predators increases. Now since there are more predators, they
will start to reduce the number of prey. When the number of prey decreases
too far, the predators won't be able to catch enough of them and the predators
will start to die of starvation. That's the cycle.
Those of you who are interested in the predator/prey relationship, might find
the following website interesting. It is a National Park Service publication
about
the moose and wolf populations on Isle Royale .
We will be studying a fictional population of rabbits and/or foxes isolated
on an island and will make the following assumptions:
- Rabbits only die by being eaten by foxes
- Foxes only die from natural causes
- The interaction between foxes and rabbits can be described by a
function.
Population Formulas
- For Rabbit Populations :
If no foxes are present the new rabbit population is calculated from
the previous population by the following equation:
PRn = PR(n-1) + BR * PR(n-1)
where :
- 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
Under these conditions the rabbit population would continually increase.
- For Fox Populations :
If no hunters are present the new fox population is calculated from the
previous population by the following equation:
PFn = PF(n-1) - DF * PF(n-1)
where:
- PFn is the new fox population after time period n
- PF(n-1) is the beginning fox population at the time
period n-1
- DF is the death rate for foxes
Under these conditions the fox population will decrease.
- For Both Foxes and Rabbits Coexisting :
When foxes can eat rabbits the population equations have another term
to describe the interaction between the two.
The population of rabbits is the same as above with a loss of
population due to foxes :
PRn = PR(n-1) + BR * PR(n-1)
- I * PR(n-1) * PF(n-1)
where :
I is the interaction constant between the foxes and the rabbits.
The population of foxes is increased with the addition of the
interaction term :
PFn = PF(n-1) - DF * PF(n-1)
+ I * PF(n-1) * PR(n-1)
Now the population of rabbits is decreased by the foxes with the
greater number of foxes causing a greater decrease in the rabbit
population. This means that the rabbit population is eventually going
to decrease and the resulting fox population will also decease. As a
result a cyclic increase and decrease in the populations are set up.
This is what is observed in the simulation.
Formulas and their descriptions courtesy of J. Barry DeRoos of messiah.edu
The Task
You are to write a program that simulates populations of rabbits, foxes or
both over time, using the formulas shown above. Certainly there can never
be a negative number of rabbits or foxes, so if the calculation of rabbits
indicates that there should be a negative number of rabbits, change the
number of rabbits to 0. We are also capping the populations at 500
individuals of each type. So if the calculations produce an answer of greater
than 500 foxes, the number of foxes should be changed to 500. It is possible
to have 500 rabbits and 500 foxes at the same time.
You will have to get input from the user for the following values:
- The original rabbit population, numRabbits (int)
- The original fox population, numFoxes (int)
- The birth rate of rabbits, birthRate (float)
- The death rate of foxes, deathRate (float)
- The interaction constant, interaction (float)
- The number of years to run the simulation, years (int)
After printing the program explanation and getting the initial values
necessary for the simulation, you will display a menu that will allow the
user to choose which kind of population to simulate or to change the
interaction constant. The choices must be:
- Simulate a Population of Rabbits Only
- Simulate a Population of Foxes Only
- Simulate a Population with Rabbits & Foxes Interacting
- Change Interaction Constant
- Quit
You will be expected to write twelve functions, other than main(), for this
project.
Here are the function prototypes:
void PrintGreeting (void);
int GetValidInt (int min, int max);
float GetValidFloat (float min, float max);
void PrintMenu (void);
void SimulateRabbits (int numRabbits, int numFoxes, float birthRate,
float interaction, int years);
void SimulateFoxes (int numRabbits, int numFoxes, float deathRate,
float interaction, int years);
void SimulateBoth (int numRabbits, int numFoxes, float birthRate,
float deathRate, float interaction, int years);
int CalculateRabbits (int numRabbits, int numFoxes, float birthRate,
float interaction);
int CalculateFoxes (int numRabbits, int numFoxes, float deathRate,
float interaction);
float ChangeInteraction (void);
void PrintHeading (int numRabbits, int numFoxes, float birthRate,
float deathRate, float interaction, int years);
void PrintTableLine (int time, int numRabbits, int numFoxes);
You MUST use these function prototypes, as shown, without modification.
Function Descriptions:
- PrintGreeting() - should print a message to the user, explaining
what the program will do.
- GetValidInt() - should prompt the user for an integer within the range
of min to max, inclusive, that are passed in as arguments. It assures the
integer entered is within that range and returns it.
- GetValidFloat() - should prompt the user for a value within the range
of min to max, inclusive, that are passed in as arguments. It assures the
value entered is within that range and returns it as a float.
- PrintMenu() - should print the menu to the screen
- SimulateRabbits() - simulates a population composed of rabbits only by
printing a table showing the populations each year. The values on each
line are found by calling CalculateRabbits().
- SimulateFoxes() - simulates a population composed of foxes only by
printing a table showing the populations each year. The values on each
line are found by calling CalculateFoxes().
- SimulateBoth() - simulates a population composed of both rabbits and foxes
by printing a table showing both populations each year. The values on each
line are found by calling CalculateRabbits() followed by calling
CalculateFoxes(). Hint: You'll need to keep the previous number of
rabbits to pass to CalculateFoxes().
- CalculateRabbits() - returns the new number of rabbits by implementing
the formula
PRn = PR(n-1) + BR * PR(n-1)
- I * PR(n-1) * PF(n-1)
but keeping the population within the range 0 <= numRabbits <= 500.
- CalculateFoxes() - returns the new number of foxes by implementing the
formula
PFn = PF(n-1) - DF * PF(n-1) +
I * PF(n-1) * PR(n-1)
but keeping the population within the range 0 <= numFoxes <= 500.
- ChangeInteraction() - allows the user to change the interaction constant
by calling GetValidFloat() and returning the new value to be used
- PrintHeading() - takes the initial values entered by the user and prints
the appropriate title, values and the column headings for the table.
- PrintTableLine() - takes the current year, the current number of rabbits
and the current number of foxes and prints them as one line of the table.
Sample Run
Since the output is long, here is a link to a separate webpage containing
the sample run.
More Details
- You MUST use the prototypes as shown without any modification.
- You MUST use separate compilation. Your files must be named
proj1.c, util.c and util.h, where proj1.c should
contain main(), and the functions that are project specific (can't
be used in other projects). The file util.c should contain the
function definitions of the modules, GetValidInt() and GetValidFloat(),
since they will be useful with other projects. util.h should contain
the function prototypes for the functions found in util.c
- You MUST use symbolic constants for all numeric values, therefor
you will need the following #defines :
#define MIN_CHOICE 1
#define MAX_CHOICE 5
#define RABBITS 1
#define FOXES 2
#define BOTH 3
#define CHANGE 4
#define QUIT 5
#define MIN_RABBITS 0
#define MAX_RABBITS 500
#define MIN_FOXES 0
#define MAX_FOXES 500
#define MIN_BIRTH_RATE 0.0
#define MAX_BIRTH_RATE 1.0
#define MIN_DEATH_RATE 0.0
#define MAX_DEATH_RATE 1.0
#define MIN_INTERACTION 0.0
#define MAX_INTERACTION 1.0
#define MIN_YEARS 1
#define MAX_YEARS 50
- The design for this project is to be given in lecture on 9/26/04
and 9/27/04. Your project MUST be written as it is designed in
class.
Submitting the Program
To submit the file you should use the command:
submit cs201 Proj1 proj1.c util.c util.h
You can check your submission by using the command:
submitls cs201 Proj1