I have no idea how to construct this program. Any help is appreciated. Thank you.
In a population, the birth rate is the percentage increase of the population due to births and the death rate is the percentage decrease of the population due to deaths. Write a program that displays the size of a population for any number of years. The program should ask for the following data:
* The starting size of a population
* The annual birth rate
* The annual death rate
* The number of years to display
Write a function that calculates the size of the population for a year. The formula is:
N = P + BP - DP
Where N is the new population size, P is the previous population size, B is the birth rate, and D is the death rate.
**Input Validation: Do not accept numbers less than 2 for the starting size. Do not accept negative numbers for birth or death rate. Do not accept numbers less than 1 for the number of years.
-------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
using namespace std;
double population(double pop, double birthRate, double deathRate);
void printPopulations(
double startPop, double birthRate, double deathRate, int numYears);
// --------------------------------
// ----- ENTER YOUR CODE HERE -----
// --------------------------------
// --------------------------------
// --------- END USER CODE --------
// --------------------------------
int main()
{
double startPop, // To hold the starting population.
birthRate, // To hold the birth rate.
deathRate; // To hold the death rate.
int numYears; // To hold the number of years to track population changes.
// Input and validate starting population
cout << "This program calculates population change.\n";
cout << "Enter the starting population size: ";
cin >> startPop;
while (startPop < 2.0)
{
cout << "Starting population must be 2 or more. Please re-enter: ";
cin >> startPop;
}
// Input and validate annual birth and death rates
cout << "Enter the annual birth rate (as % of current population): ";
cin >> birthRate;
while (birthRate < 0)
{
cout << "Birth rate percent cannot be negative. Please re-enter: ";
cin >> birthRate;
}
birthRate = birthRate / 100; // Convert from % to decimal.
cout << "Enter the annual death rate (as % of current population): ";
cin >> deathRate;
while (deathRate < 0)
{
cout << "Death rate percent cannot be negative. Please re-enter: ";
cin >> deathRate;
}
deathRate = deathRate / 100; // Convert from % to decimal.
cout << "For how many years do you wish to view population changes? ";
cin >> numYears;
while (numYears < 1)
{
cout << "Years must be one or more. Please re-enter: ";
cin >> numYears;
}
printPopulations(startPop, birthRate, deathRate, numYears);
return 0;
} // End of main function.
1 answer
N=P+BP-DP
both B and D have to be divided by 100 to get the real fraction.
The header for population has already been defined, all you need to do is to fill in the required formula as given and return the result, as follows:
double population(double pop, double birthRate, double deathRate){
return pop*(1+(birthrate-deathRate)/100);
}
You can do that similarly for printPopulation. Post what you've got if you need help