//Castulo Jason Quintero CSC5 Chapter 6, Pg. 373, #15
//
/**************************************************************************
*
* Calculate Population Size
* ________________________________________________________________________
* This program recieves as user input the current population
* size, annual birth rate, annual death rate, and years
* requested to calculate the new population size each year.
*
* Computation is based on formula:
* newPopulationsize = previousPopulationsize +
* (birthRate * previousPopulationsize) -
* (deathRate * previousPopulationsize)
* ________________________________________________________________________
* INPUT
* previousPopulationsize : Previous population size
* birthRate : Annual birth rate percentage
* deathRate : Annual death rate percentage
* year : Years requesting to be displayed
*
* OUTPUT
* year : Year being displayed with loop
* newPopulationsize : New size of population each year
*************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
// Prototype
float popSize(float, float, float);
// Main function
int main()
{
float newPopulationsize; // New population size
float previousPopulationsize = 0; // Previous population size
float birthRate; // Annual birth rate percentage
float deathRate; // Annual death rate percentage
float year; // Years requesting to be displayed
// Input for starting population
cout << "Please enter the starting size of the population: \n";
cin >> previousPopulationsize;
// Input validation
while (previousPopulationsize < 2)
{
cout << "Please enter a starting population that is atleast "
<< "two people. \n";
cin >> previousPopulationsize;
}
// Input annual birth rate percentage
cout << "Please enter the annual birth rate percentage: \n";
cin >> birthRate;
// Input validation
while (birthRate < 0)
{
cout << "Please enter a non negative number for the birth rate \n";
cin >> birthRate;
}
// Input annual death rate percentage
cout << "Please enter the annual death rate percentage: \n";
cin >> deathRate;
// Input validation
while (deathRate < 0)
{
cout << "Please enter a non negative number for the birth rate \n";
cin >> deathRate;
}
// Input years to be displayed
cout << "Please enter the number of years to be displayed: \n";
cin >> year;
// Input validation
while (year < 1)
{
cout << "Please enter a value greater than 1 for the years to "
<< "be displayed. \n";
cin >> year;
}
// For loop to give value for each year
for (int i = 1; i <= year; i++)
{
newPopulationsize = popSize(previousPopulationsize, birthRate,
deathRate);
previousPopulationsize = newPopulationsize;
cout << setprecision(0) << fixed;
cout << "The population for year " << i << " was "
<< newPopulationsize << endl;
}
return 0;
}
// Value return function and calculations
float popSize (float P, float B, float D)
{
float N;
B *= .01; // Converts to percentage
D *= .01; // Converts to percentage
N = P + (B * P) - (D * P); // Formual to get new population
return N; // Return value of N
}