//Eric Bernal CS1A Chapter 6, P. 373, #15
//
/*******************************************************************************
* CALCULATE POPULATION SHIFT
* _____________________________________________________________________________
* This program calculates the shift in a population based off of a given number
* of years, annual birth rate, and the annual death rate.
* _____________________________________________________________________________
* Input
* popBefore : The population before the shift.
* birthRate : The annual birth rate as a percentage.
* deathRate : The annual death rate as a percentage.
* years : The number of years that has passed.
*
* Output
* popAfter : The population after the shift.
******************************************************************************/
#include <iostream>
using namespace std;
// Function Prototype
float popShift(int before, float birthRate, float deathRate);
int main() {
// Data Dictionary - Initialize all Variables
float popBefore; //INPUT : The population total before
float birthRate; //INPUT : The annual birth rate as a percentage.
float deathRate; //INPUT : The annual death rate as a percentage.
float years; //INPUT : The number of years of change
float popAfter; //OUTPUT: The population total after.
// Obtain all values prompt
cout << "Please enter the following: \nPopulation: \nBirth Rate: \nDeath Rate: "
<< "\n Years: \nPlease enter the values a positive whole numbers." << endl;
cin >> popBefore >> birthRate >> deathRate >> years;
//INPUT VALIDATION
if (popBefore < 1)
{
cout << "Please enter a population greater than 0: " << endl;
cin >> popBefore;
}
if (birthRate <= 0)
{
cout << "Please enter a whole number greater than 0." << endl;
cin >> birthRate;
}
if (deathRate <= 0)
{
cout << "Please enter a whole number greater than 0." << endl;
cin >> deathRate;
}
if (years < 1)
{
cout << "Please enter a whole number greater than 0." << endl;
cin >> years;
}
// Create Population Table Chart
popAfter = popBefore;
cout << "Year 0: " << popAfter << endl;
// For loop to count the years
for (int i = 1; i <= years; i++)
{
popAfter = popShift(popAfter, birthRate, deathRate); //Function Call
cout << "Year " << i << ": " << popAfter << endl;
}
return 0;
}
/******************************************************************************
* The purpose of the following function is to calculate the population shift.
******************************************************************************/
float popShift(int before, float birthRate, float deathRate)
{
return before * (1 + (birthRate/100) - (deathRate/100));
}