Hey guys & gals,
I have a homework problem, and I'm having problems looping the month. I will share the problem, then what I've got so far. Thank you in advance for any advice :)
The problem is a typical wolf/rabbit population, but the solution asks for the population every 3 months with the exception of a termination due to lack of rabbit population (totalRabbits < 0.05):
A certain island supports rabbits and wolves as its only animal forms.
Write a program which simulates fluctuation in population by month. Read in values for Rabbits and Wolves which represent the initial number of rabbits and wolves, respectively, at the start of the first month. (For the purpose of this simulation, we allow fractional parts of animals so use real variables.)
The number of rabbits and wolves born during the month is Rabbits/5 and Wolves/10 respectively.
The number of wolves which die of starvation during the month is 200 * Wolves/Rabbits.
The number of rabbits which die of starvation is 0.00002 * Rabbits2.
The number of rabbits which are eaten by wolves is 0.009 * Rabbits * Wolves.
Run the simulation for 120 months using a looping construct but print the month, number of rabbits, and number of wolves only every three months. The calculations for a particular month should all be based on the values of Rabbits and Wolves at the end of the previous month.
INPUT: Initial numbers of Rabbits and Wolves
OUTPUT: 1) Initial values of Rabbits and Wolves
2) A tabular report as follows:
INITIAL RABBITS = xxxx.x
INITIAL WOLVES = xxx.x
MONTH RABBITS WOLVES
3 xxx.x xxx.x
6 xxx.x xxx.x
. . .
. . .
120 xxx.x xxx.x
Using a different looping construct from the one used above, process at least three separate cases of Rabbits and Wolves with one run of your program; the first with Rabbits = 5000 and Wolves = 25, and the other two with values of your own choice. If the number
of Rabbits becomes less than 0.05 during the simulation, print an appropriate error message along with the current values of month (even if not divisible by 3), Rabbits and Wolves and then terminate the case. If the number of Wolves becomes less than 0.05, perform a similar action. Be sure and include a case of data that will generate an error condition. Include appropriate labels and print all values with one place to the right of the decimal.
If a value less than 0.05 occurred for the Wolves during the 13th month, the output could look as follows:
INITIAL RABBITS = xxx.x
INITIAL WOLVES = xxx.x
MONTH RABBITS WOLVES
3 xxx.x xxx.x
6 xxx.x xxx.x
9 xxx.x xxx.x
12 xxx.x xxx.x
13 xxx.x xxx.x
Wolves vanished!! Case Terminated
This is what I've got so far. I'm not very worried about the format or appearance right now, just the calculations and loops. I suspect I might have to put the statement output between the last two brackets, out of the for loop. Thanks again!
#include <iostream> //preprocessor
#include <iomanip> //needed for setw
using namespace std; //namespace declaration
int main() //begin main function
{
float rabbitsBorn, wolvesBorn, rabbitsEaten;
float wolf_pop, rabbit_pop;
float totalRabbits;
float totalWolves;
int month, monthTerminated;
month = 0;
cout << "Enter the starting rabbit population : ";
cin >> rabbit_pop;
cout << "Enter the starting fox population : ";
cin >> wolf_pop;
rabbitsBorn = (rabbit_pop/5);
wolvesBorn = (wolf_pop/10);
wolvesStarve = (200*((wolvesBorn+wolves)/(rabbitsBorn+rabbits)));
rabbitsStarve = (0.00002*((rabbitsBorn+rabbits)*(rabbitsBorn+rabbits)));
rabbitsEaten = (0.009*((rabbits+rabbitsBorn)-rabbitsStarve));
cout << setw(28) << "INITIAL RABBITS: " << rabbit_pop;
cout << setw(28) << "INITIAL WOLVES: " << wolf_pop;
// Run the simulation for 120 months; every 3 months, print out population
for (month=0; month <= 120; month+=3)
{
// Make sure it doesn't go outside limits
if (totalRabbits < 0.05)
monthTerminated=month;
cout << "\nAll rabbits have vanished! Program terminated!" << endl;
break;
//totals - The equations for the total population of wolves & rabbits
//_________________
// How many wolves don't starve?
totalWolves = ((wolf_pop+wolvesBorn)-wolvesStarve);
// Can't be more survivors than the original population
if (totalWolves > wolf_pop)
{
totalWolves = wolf_pop;
}
// How many rabbits survived?
totalRabbits = ((rabbitsBorn+rabbit_pop)-(rabbitsStarve+rabbitsEaten));
cout << setw(15) << "MONTH" << month;
cout << setw(17) << "RABBITS" << //totalRabbits;
cout << setw(17) << "WOLVES" << //totalWolves;
cout << endl;
// Now they breed for the next month
rabbit_pop = (totalRabbits*rabbitsBorn);
// Now the wolves breed
wolf_pop = (totalWolves*wolvesBorn);
}
}