//Andres Guzman CSC5 Chapter 6, P. 373, #15
//
/*******************************************************************************
*
* CALCULATE POPULATION
* ______________________________________________________________________________
* This program will calculate a population after an inputed set of years
*
* Computation:
*
* N = P + BP - BP
* ______________________________________________________________________________
* INPUT
*
* pop = Input for population
* birth = Input for birth rate
* death = Input for death rate
* years = Input for number of years
* OUTPUT
* newpop = New population
*******************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
int pop; //Global Variable
double birth; //Global Variable
double death; //Global Variable
int years; //Global Variable
double population(); //function prototype
int main ()
{
cout << "\tPopulation" << endl;
cout << "Population Starting Size: ";
cin >> pop;
//
//While loop to validate population input
while(pop < 2) //pop cannot be less than 2
{
cout << "Invalid Starting Size." << endl;
cout << "Population Starting Size: ";
cin >> pop;
}
cout << "\nAnnual Birth Rate: ";
cin >> birth;
//
//While loop to validate birth rate input
while (birth < 0) //birth cannot be less than 0
{
cout << "Invalid Rate." << endl;
cout << "Annual Birth Rate: ";
cin >> birth;
}
cout << "\nAnnual Death Rate: ";
cin >> death;
//
//While loop to validate death rate input
while (death < 0) //death cannot be less than 0
{
cout << "Invalid Rate." << endl;
cout << "Annual Death Rate: ";
cin >> death;
}
cout << "\nYears to Display: ";
cin >> years;
cout << endl;
//
//While loop to validate years input
while (years < 1) // Years cannot be less than 1
{
cout << "Invalid Years." << endl;
cout << "Years to Display: ";
cin >> years;
cout << endl;
}
//
//Calling function
population(); //Function Call
}
/*******************************************************************************
* Creating defintion for function population. It will use the inputs from the
* global variables to caluclate the new population using the formula inside and
* then will output a loop with the result for each year.
******************************************************************************/
double population()
{
double newpop = pop; //Transfers pop value to newpop for easier
//calculation.
for (int i = 1; i <= years; i++) //For loop to loop each inputed year
{
newpop = newpop + (birth * newpop) - (death * newpop); //formula for
//new population
//
//If statement to remove any negative results
if(newpop < 0) //
{
newpop = 0; //If its less than 0, then the value becomes 0
}
//
//Outputting results and allowing for decimal points
cout << fixed << setprecision(2) << "Year " << i << ": ";
cout << newpop << endl;
}
return newpop; //returns the value of newpop
}