//Zachary Abdollahi CS1A Chapter 6, P. 373, #15
//
/*******************************************************************************
*
* COMPUTE POPULATION GROWTH
*
* This program calculates and displays the projected size of a population over
* a specified number of years based on annual birth and death rates.
*
* Computation is based on the formula:
* N = P + (B * P) - (D * P)
*
* INPUT
* startingPop : Starting size of the population
* birthRate : Annual birth rate (as a percentage)
* deathRate : Annual death rate (as a percentage)
* years : Number of years to project
*
* OUTPUT
* newPop : Calculated population size for each year
*
******************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
// Function Prototypes
int calculatePopulation(int pop, double birthRate, double deathRate);
int main()
{
int population; // Starting/current population size
double birthRate; // Annual birth rate
double deathRate; // Annual death rate
int years; // Number of years to display
// Get the starting population (must be at least 2)
cout << "Enter the starting population size: ";
cin >> population;
while (population < 2)
{
cout << "Population must be at least 2. Enter starting population: ";
cin >> population;
}
// Get annual birth rate (cannot be negative)
cout << "Enter the annual birth rate (e.g., 0.05 for 5%): ";
cin >> birthRate;
while (birthRate < 0)
{
cout << "Birth rate cannot be negative. Enter birth rate: ";
cin >> birthRate;
}
// Get annual death rate (cannot be negative)
cout << "Enter annual death rate (e.g., 0.02 for 2%): ";
cin >> deathRate;
while (deathRate < 0)
{
cout << "Death rate cannot be negative. Enter death rate: ";
cin >> deathRate;
}
// Get number of years (must be at least 1)
cout << "Enter the number of years to project: ";
cin >> years;
while (years < 1)
{
cout << "Years must be at least 1. Enter number of years: ";
cin >> years;
}
// Display table header
cout << "\n----------------------------------------" << endl;
cout << "Year\t\tProjected Population" << endl;
cout << "----------------------------------------" << endl;
// Calculate and display populatiob for each year
for (int year = 1; year <= years; year++)
{
population = calculatePopulation(population, birthRate, deathRate);
cout << year << "\t\t" << population << endl;
}
return 0;
}
/******************************************************************************
* calculatePopulation *
* Accepts the previous population, birt rate, and death rate as arguments, *
* calculates the new population size using N = P + BP -DP, and returns the *
* result as an integer. *
*****************************************************************************/
int calculatePopulation(int pop, double birthRate, double deathRate)
{
return pop + (birthRate * pop) - (deathRate * pop);
}