1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include <iostream>
#include <iomanip>
using namespace std;
void calculatepop(int, float, float, int,int);
int main()
{
int years;
int population;
float birthrate;
float deathrate;
int newpopulation = 0;
cout << "What is the current population?\n";
cin >> population;
// Catches non-realistic input
while (population < 2)
{
cout << "Please specify a number larger than 1.\n";
cin >> population;
}
cout << "What is the annual birth rate? (percentage)\n";
cin >> birthrate;
// Catches invalid input
while (birthrate < 0.0)
{
cout << "Please specify a non-negative percentage.\n";
cin >> birthrate;
}
cout << "What is the annual death rate? (percentage)\n";
cin >> deathrate;
// Catches invalid input
while (deathrate < 0.0)
{
cout << "Please specify a non-negative percentage.\n";
cin >> birthrate;
}
cout << "For how many years will this increase take place?\n";
cin >> years;
// Catches invalid input
while (years < 1)
{
cout << "Please specify a number larger than zero.\n";
cin >> years;
}
cout << "" << endl;
cout << "YEAR POPULATION\n";
cout << "---------------------------------\n";
calculatepop(population, birthrate, deathrate, years,1);
return 0;
}
// Using recursion instead of fo loop
void calculatepop(int population, float birthrate, float deathrate, int years,int start)
{
if (start > years)
return;
population += (population * (birthrate * .01)) - (population * deathrate * .01);
cout << start << " " << population << endl;
start++;
calculatepop(population, birthrate, deathrate, years, start);
}
|