// Timothy Price CS1A Ch5 Challenge 11
/*******************************************************************************
* Calculate population
*
* This program receives values and calculates projected growth.
*
* Inputs:
* Statrting number of organisms
* Average daily growth percentage
* Number of days population will grow
*
* Outputs:
* Table displaying population size per day
******************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
// Data dictionary:
int days;
double dailypercent;
double population;
double increaserate;
cout << "Enter the starting number of organisms (minimum 2): ";
cin >> population;
cout << population << " organisms." << endl;
cout << "Enter the average daily population growth percentage: ";
cin >> dailypercent;
cout << dailypercent << "%" << endl;
cout << "Enter the number of days they will multiply (minimum 1): ";
cin >> days;
cout << days << " days." << endl;
// Input validation:
if (population < 2)
cout << "Invalid input. Starting size must be at least 2: ";
if (dailypercent < 0)
cout << "Invalid input. Daily increase cannot be negative: ";
if (days < 1)
cout << "Invalid input. Number of days must be at least 1: ";
// Change percentage to decimal value:
increaserate = dailypercent / 100.0;
if (population >= 2 && dailypercent >= 0 && days >= 1) {
// Display table header:
cout << "\n--- Population Growth Projection ---" << endl;
cout << "Day\t\t\tPopulation Size" << endl;
cout << "-----------------------------------" << endl;
// Display population for each day:
for (int day = 1; day <= days; ++day) {
// Used static_cast to truncate the decimal value to find whole number:
cout << day << "\t\t\t" << static_cast<int>(population) << endl;
// Calculate the population for the next day
population += (population * increaserate);
}
}
return 0;
}