Write a C++ program using the while loop to calculate when the population ceases breeding and thus dies out.You do not have to input any values.
birth rate = 5%
death rate = 17%
starting population = 1500
population threshold = 165
I'm having trouble looping the program so that it can continue on after year one. The output is supposed to look like this:
YEAR ALIVE AT START NEW BIRTHS KILLED ALIVE AT END
1 1500 75 267 1308
2 1308 65 233 1140
3 1140 57 203 994
....and so on until it reaches 165 for the population. I have already figured out the formulas and I have the year 1 all set. My problem is my program keeps looping an infinite number of years with the data for year one repeated, instead of taking the population at the end of the year and starting the new year with it. Any help will be greatly appreciated.
[code]
#include <iomanip>
#include <string>
#include <iostream>
using namespace std;
int main()
{
//declare variables
int year;
int population;
int startPopulation;
int newBirths;
int killed;
int newPopulation;
const int birthPercentage = 5;
const int killPercentage = 17;
const int threshold = 165;
int aliveatend;
int begin;
year = 1;
startPopulation = 1500;
population = startPopulation;
cout << setfill(' ') << setw(1) << "|" << setw(5) << left << ("YEAR") << setw(1) << "|" << setw(15) << left << "ALIVE AT START" << setw(1) << "|" << setw(10) << left << "NEW BIRTHS" << setw(1) << "|" << setw(5) << left << "KILLED" << setw(1) << "|" << setw(5) << left << "ALIVE AT END" << setw(1) << "|" << endl;
thank you! you did help. I just tweaked the formula for dead to get the numbers I wanted. this is the end result I got. btw, setw sets the width between the numbers for when you're making a chart.
[/code]
#include <iostream>
using namespace std;
int main() {
int population = 1500;
int birth_rate = 0.05;
int death_rate = 0.17;
int birth, dead, alive;
int year=1;
cout << "Year " << "Alive at start " << "New Births ";
cout << "Killed " << "Alive At The End" << endl;
while( population >= 165 ) {
birth = population * 0.05;
dead = ((population + birth) * 0.17);
alive = population + birth - dead;
cout << year << " " << population << " " << birth;
cout << " " << dead << " " << alive << endl;
++year;
population = alive;
}
}
As someone said before, to use birth_rate and dead_rate and not having to put 0.05 and 0.17, you should double them and not int. But as did before it is ok too. And thanks about that thing of set, now i understand what that does, but i will search for a more efficient way to use it, more cleaner if there is one.