// This program asks for the user to enter the starting size of a population, the annual birth rate, the annual death rate, and the number of years to display.
// Following user input, the program should calculate the size of the population for a year using the formula:
//
// N = P + BP - DP
//
// N = New Population Size
// P = Previous Population Size
// B = Birth Rate
// D = Death Rate
//
// Input Validations:
// Do not accept numbers less than 2 for the starting size.
// Do not accept negative numbers for birth rate or death rate.
// Do not accept numbers less than 1 for the number of years.
//
//Program from Starting Out With C++ from Control Structures through Objects (Ninth Edition) by Tony Gaddis(2018) pg. 377-378, Problem #16.
//
// Programmed by Devin Dahlberg, CIS 251 Student
// February 15, 2024
#include <iostream>
#include <iomanip>
using namespace std;
// Function to calculate the new population and repeats for the required amount of years
double calculate(double startingP, double birth, double death, double years) {
double newP, previousP = startingP;
for (int count = 1; count < years + 1; count++) {
newP = previousP + ((birth / 100) * previousP) - ((death / 100) * previousP);
cout << setprecision(0) << fixed;
cout << "The population for year " << count << " is " << newP << endl;
cout << " " << endl;
previousP = newP;
}
newP = startingP;
previousP = newP;
return newP;
}
// Main function that prompts the user enter starting population, birth rate, death rate, and how many years they would like to see.
// Includes Input Validations.
int main()
{
double startingP, birth, death, years;
cout << "Enter the Starting Size of a Population: " << endl;
cin >> startingP;
cout << " " << endl;
while (startingP < 2) {
cout << "Invalid Starting Population. " << endl;
cout << " " << endl;
cout << "Enter the Starting Size of a Population: " << endl;
cin >> startingP;
cout << " " << endl;
}
cout << "Enter the Annual Birth Rate: " << endl;
cin >> birth;
cout << " " << endl;
while (birth < 0) {
cout << "Invalid Annual Birth Rate. " << endl;
cout << " " << endl;
cout << "Enter the Annual Birth Rate: " << endl;
cin >> birth;
cout << " " << endl;
}
cout << "Enter the Annual Death Rate: " << endl;
cin >> death;
cout << " " << endl;
while (death < 0) {
cout << "Invalid Annual Death Rate. " << endl;
cout << " " << endl;
cout << "Enter the Annual Death Rate: " << endl;
cin >> death;
cout << " " << endl;
}
cout << "Enter the Number of Years to Display: " << endl;
cin >> years;
cout << " " << endl;
while (years < 1) {
cout << "Invalid Number of Years. " << endl;
cout << " " << endl;
cout << "Enter the Number of Years to Display: " << endl;
cin >> years;
cout << " " << endl;
}
calculate(startingP, birth, death, years);
return 0;
}