//Dione Torrevillas CS1A Chapter 6, P. 373, #15
//
/****************************************************************
*
* CALCULATE POPULATION GROWTH
*_______________________________________________________________
* This program computes the population size over a number of
* years based on the starting population, annual birth rate,
* and annual death rate. A function calculates the population
* for each year.
*
* Computation is based on the formula:
* New Population = Previous Population +
* (Birth Rate * Previous Population) -
* (Death Rate * Previous Population)
*_______________________________________________________________
* INPUT
* population : Starting population size
* birthRate : Annual birth rate percentage
* deathRate : Annual death rate percentage
* years : Number of years to display
*
* OUTPUT
* population : Population size for each year
*
****************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototype
double calculatePopulation(double, double, double);
int main()
{
double population; //INPUT - Starting population
double birthRate; //INPUT - Annual birth rate
double deathRate; //INPUT - Annual death rate
int years; //INPUT - Number of years
int year; //OUTPUT - Year being displayed
// Get the starting population
cout << "Enter the starting population: ";
cin >> population;
// Validate the starting population
while (population < 2)
{
cout << "Enter a population of at least 2: ";
cin >> population;
}
// Get the annual birth rate
cout << "Enter the annual birth rate (%): ";
cin >> birthRate;
// Validate the birth rate
while (birthRate < 0)
{
cout << "Enter a nonnegative birth rate: ";
cin >> birthRate;
}
// Get the annual death rate
cout << "Enter the annual death rate (%): ";
cin >> deathRate;
// Validate the death rate
while (deathRate < 0)
{
cout << "Enter a nonnegative death rate: ";
cin >> deathRate;
}
// Get the number of years
cout << "Enter the number of years: ";
cin >> years;
// Validate the number of years
while (years < 1)
{
cout << "Enter at least 1 year: ";
cin >> years;
}
// Convert percentages to decimal values
birthRate = birthRate / 100;
deathRate = deathRate / 100;
// Display the table heading
cout << endl;
cout << "Year Population" << endl;
cout << "---------------------" << endl;
// Calculate and display population for each year
for (year = 1; year <= years; year++)
{
// Call function to calculate new population
population = calculatePopulation(population,
birthRate,
deathRate);
// Display the population for the year
cout << setw(4) << year
<< setw(17) << fixed << setprecision(0)
<< population << endl;
}
return 0;
}
// Function definition
// Calculates and returns the new population
double calculatePopulation(double population,
double birthRate,
double deathRate)
{
double newPopulation; // New population size
// Calculate the population for one year
newPopulation = population +
(birthRate * population) -
(deathRate * population);
return newPopulation;
}