c++ Objective: To use two loops in a program to calculate population growth. Thi
ID: 3728212 • Letter: C
Question
c++
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:
Explanation / Answer
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.