Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is C++. In a population, the birth rate is the percentage increase of the p

ID: 3573171 • Letter: T

Question

This is C++. In a population, the birth rate is the percentage increase of the population due to births, and the death rate is the percentage decrease due to deaths. Write a C++ 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

The formula for calculating the size of a population for a year is

where N is the new population size, P is the previous population size, BP is the birth population for the year, and DP is the death population for the year.

Input validation: Do not accept numbers less than 2 for the starting population size. Do not accept numbers less than 1 for the number of years. You can assume that inputs for birth rate and death rate are always valid (non-negative).

The program should display a table that shows the new population after each year starting by year 1 and ending by the number of years entered by the user. Format floating-point numbers to show two digits after the decimal point. Make sure that columns are left aligned. Use 15 spaces for displaying the first column and 10 spaces for the second column. Sample program run is as follows, where user input is shown in red color:

Another sample program run is as follows:

Explanation / Answer

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
int i;
float N=0.0;//N is the new population size
float P ;// P is the previous population size,
float BP ; //BP is the birth population for the year.
float DP ;// DP is the death population for the year.
int year;

X:
cout <<"Enter population start size :" ;
cin>>P;
if(P <2)
{
cout<<"Invalid input. Try again"<< endl;
goto X;

}
cout << "Enter annual birth rate :";
cin>>BP;
cout << "Birth rate is :" << BP/100.0<< endl;

cout << "Enter annual death rate :";
cin>>DP;
cout << "Death rate is:" << DP/100.0<< endl;
Y:
cout << "Enter number of years :";
cin>>year;
if(year<1)
{
cout<<"Invalid input. Try again"<< endl;
goto Y;
}

cout<<"After year"<<" "<<"Population"<<endl;
for(i=0;i<year;i++)
{
std::cout << std::fixed;
std::cout << std::setprecision(2);   
N = P + P*(BP - DP)/100;
cout<<i+1<<" "<<N<<endl;
P=N;

}
return 0;
}

-----------------

output sample:-

Enter population start size :100
Enter annual birth rate :5
Birth rate is :0.05
Enter annual death rate :2
Death rate is:0.02
Enter number of years :7
After year Population
1 103.00
2 106.09
3 109.27
4 112.55
5 115.93
6 119.41
7 122.99

----------------

output sample 2:-

Enter population start size :0
Invalid input. Try again
Enter population start size :1
Invalid input. Try again
Enter population start size :1000000
Enter annual birth rate :7.5
Birth rate is :0.075
Enter annual death rate :3.5
Death rate is:0.035
Enter number of years :-3
Invalid input. Try again
Enter number of years :0
Invalid input. Try again
Enter number of years :5
After year Population
1 1040000.00
2 1081600.00
3 1124864.00
4 1169858.50
5 1216652.88

---------------------------------------------------------------------------------------------

If you have any query, please feel free to ask.

Thanks a lot.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote