//Devin Scheu CS1A Chapter 6, P. 173, #15
//
/**************************************************************
*
* CALCULATE POPULATION GROWTH OVER YEARS
* ____________________________________________________________
* This program determines the population size over a specified
* number of years based on starting size, birth rate, and death rate.
* ____________________________________________________________
* INPUT
* startingPopulation : The initial population size
* birthRate : The annual birth rate percentage
* deathRate : The annual death rate percentage
* numYears : The number of years to calculate
*
* OUTPUT
* populationTable : The population size for each year
*
**************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
// Function to calculate population for a year
double calculatePopulation(double previousPopulation, double birthRate, double deathRate) {
double births = previousPopulation * (birthRate / 100);
double deaths = previousPopulation * (deathRate / 100);
return previousPopulation + births - deaths;
}
int main () {
//Variable Declarations
double startingPopulation; //INPUT - The initial population size
double birthRate; //INPUT - The annual birth rate percentage
double deathRate; //INPUT - The annual death rate percentage
int numYears; //INPUT - The number of years to calculate
double currentPopulation; //OUTPUT - The population size for each year
string populationTable; //OUTPUT - The population size for each year
//Prompt for Input and Echo
cout << "Enter the starting population size: ";
cin >> startingPopulation;
while (startingPopulation < 2) {
cout << "\nError: Please enter a number greater than or equal to 2: ";
cin >> startingPopulation;
}
cout << startingPopulation << endl;
cout << "Enter the annual birth rate (%): ";
cin >> birthRate;
while (birthRate < 0) {
cout << "\nError: Please enter a non-negative birth rate: ";
cin >> birthRate;
}
cout << birthRate << "%" << endl;
cout << "Enter the annual death rate (%): ";
cin >> deathRate;
while (deathRate < 0) {
cout << "\nError: Please enter a non-negative death rate: ";
cin >> deathRate;
}
cout << deathRate << "%" << endl;
cout << "Enter the number of years: ";
cin >> numYears;
while (numYears < 1) {
cout << "\nError: Please enter a number greater than or equal to 1: ";
cin >> numYears;
}
cout << numYears << endl;
//Separator and Output Section
cout << "-------------------------------------------------------" << endl;
cout << "OUTPUT:" << endl;
//Display Header
cout << fixed << setprecision(0);
cout << left << setw(15) << "Year" << right << setw(20) << "Population" << endl;
cout << "-----------------------------------------------" << endl;
//Calculate and Display Population for Each Year
currentPopulation = startingPopulation;
for (int year = 0; year <= numYears; year++) {
if (year == 0) {
cout << left << setw(15) << "Starting" << right << setw(20) << static_cast<long long>(currentPopulation) << endl;
} else {
currentPopulation = calculatePopulation(currentPopulation, birthRate, deathRate);
cout << left << setw(15) << year << right << setw(20) << static_cast<long long>(currentPopulation) << endl;
}
}
} //end of main()