//Matthew Santos    CS1A     Ch. 6, Pg. 373, #15
/***********************************************
 * 
 * CALCULATE POPULATION
 * _____________________________________________
 * Calculates and displays the population size
 * over any number of years based off the
 * starting size and annual birth and death rate.
 * _____________________________________________
 * INPUT
 *      startingPop : starting population
 *      birthRate   : birth rate as a decimal
 *      deathRate   : death rate as a decimal
 *      years       : years simulated
 * 
 * OUTPUT
 *      population for the given years
 ***********************************************/
#include <iostream>
#include <iomanip>
using namespace std;
 
double calculatePopulation(double, double, double);
 
int main() {
    double startingPop;
    double birthRate;
    double deathRate;
    int years;
 
    // Input for starting population
    cout << "Enter the starting size of the population: ";
    cin >> startingPop;
    while (startingPop < 2)
    {
        cout << "Starting size must be at least 2. Enter again: ";
        cin >> startingPop;
    }
 
    // Input for birth rate
    cout << "Enter the annual birth rate (as a decimal, example 0.05 for 5%): ";
    cin >> birthRate;
    while (birthRate < 0)
    {
        cout << "Birth rate cannot be negative. Enter again: ";
        cin >> birthRate;
    }
 
    // Input for death rate
    cout << "Enter the annual death rate (as a decimal, example 0.02 for 2%): ";
    cin >> deathRate;
    while (deathRate < 0)
    {
        cout << "Death rate cannot be negative. Enter again: ";
        cin >> deathRate;
    }
 
    // Input for number of years
    cout << "Enter the number of years to display: ";
    cin >> years;
    while (years < 1)
    {
        cout << "Number of years must be at least 1. Enter again: ";
        cin >> years;
    }
 
    // Display the population each year
    cout << fixed << setprecision(0);
    cout << "\nYear\tPopulation\n";
    cout << "-----------------\n";
 
    double population = startingPop;
    for (int year = 1; year <= years; ++year)
    {
        population = calculatePopulation(population, birthRate, deathRate);
        cout << year << "\t" << population << endl;
    }
 
    return 0;
}
 
double calculatePopulation(double P, double B, double D)
{
    return P + (B * P) - (D * P);
}