//Cameron Pham CS1A Chapter 6, p. 373, #15
//
/******************************************************************************
* CALCULATE POPULATION SIZE
* ______________________________________________________________________________
* This program accepts an integer value for the starting size of a population,
* number of years, and floating point values for the annual birth and death
* rate.
* ______________________________________________________________________________
* INPUT
* Population size // Starting size of population
* Annual birth rate // Yearly rate at which people are born
* Annual death rate // Yearly rate at which people pass away
* Years // Period of time to calculate population
*
* OUTPUT
* Size of population // Calculated size of a population after given years
*
*******************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototype
double calculatePopulation(double population, double birthRate,
double deathRate);
int main()
{
// Declare variables
double population;
double birthRate;
double deathRate;
int years;
// Get the starting population
cout << "Enter the starting population size: ";
cin >> population;
cout << endl;
// Validate the starting population
while (population < 2)
{
cout << "Population must be at least 2. Enter again: ";
cin >> population;
}
// Get the annual birth and death rates
cout << "Enter the annual birth rate (percentage): ";
cin >> birthRate;
cout << endl;
cout << "Enter the annual death rate (percentage): ";
cin >> deathRate;
cout << endl;
// Get the number of years
cout << "Enter the number of years to display: ";
cin >> years;
cout << endl;
// Validate the number of years
while (years < 1)
{
cout << "Years must be at least 1. Enter again: ";
cin >> years;
}
// Convert percentages to decimal rates
birthRate /= 100.0;
deathRate /= 100.0;
// Display the population
cout << fixed << setprecision(2);
population = calculatePopulation(population, birthRate, deathRate);
cout << years << " year(s): " << population << " people." << endl;
return 0;
}
// Function to calculate the new population size
double calculatePopulation(double population, double birthRate,
double deathRate)
{
double newPopulation;
newPopulation = population + birthRate * population - deathRate * population;
return newPopulation;
}