Objective: To use two loops in a program to calculate population growth. This pr
ID: 3630364 • Letter: O
Question
Objective: To use two loops in a program to calculate population growth.
This program is about an imaginary animal called a jackalope. Each generation, the jackalope population increases by 3% due to births and decreases by 1% due to deaths. Both the number of jackalopes who die and who are born will be rounded down (truncated) to an integer. See below for how to do this. So using a simple formula for calculating the number of jackalopes after a generation:
If you start with 200 jackalopes, then 3% more are born, increasing the number to 206. 1% of the 206 die, decreasing the number to 204.
If you were to start with 132 jackalopes, then 3 would be born (132 * 0.03 = 3.96, rounded down to 3) and of the 135, 1 would die, leaving us with 134 jackalopes. The following generation, 3% of the 134 would produce 4 births, and 1% of 138 would produce 1 death, leaving us with 137. Note that this isn't the same result as if we simply add 2% each year.
The program should behave like this:
How many jackalopes do you have? 200
How many generations do you want to wait? 1
If you start with 200 jackalopes and wait 1 generations,
you'll end up with a total of 204 of them.
Do you want to calculate another population? y
How many jackalopes do you have? 132
How many generations do you want to wait? 2
If you start with 132 jackalopes and wait 2 generations,
you'll end up with a total of 137 of them.
Do you want to calculate another population? y
How many jackalopes do you have? 40
How many generations do you want to wait? 100
If you start with 40 jackalopes and wait 100 generations,
you'll end up with a total of 291 of them.
Do you want to calculate another population? n
Here are the rules and hints:
Your program should allow the user to repeat the calculations as many times as he or she wishes (as above)
You must test and demonstrate your program with the above 3 test cases (and others if you wish)
To truncate a double variable to an integer value, use a type cast. For example:
int i;
double d = 3.6;
i = (int)d; // i now stores 3
Your grade will be based upon your program's correctness, clarity, programming style, and efficiency. So take care with things like indentation, comments, variable names, etc.
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
int jackNum;
int generations;
int total=0;
double tmp=0;
char next='y';
while(next=='y'){
total=0;
tmp=0;
cout<<"How many jackalopes do you have? ";
cin>>jackNum;
cout<<"How many generations do you want to wait? ";
cin>>generations;
total=jackNum;
for (int x =0;x<generations;x++){
tmp=total*0.03;
total+=(int)tmp;
tmp=total*0.01;
total-=(int)tmp;
}
cout<<"If you start with "<<jackNum <<" jackalopes and wait ";
cout<< generations<<" generation(s), you'll end up with a total of ";
cout<<total<<" of them."<<endl;
cout<<" Do you want to calculate another population?";
cin>>next;
}
return 0;
} // end main
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.