Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I made a program for an assignment on myprogramminglab and its almost correct but there is a slight error can anyone help me figure out

I made a program for an assignment on myprogramminglab and its almost correct but there is a slight error can anyone help me figure out what it is?

I don't choose the inputed values, it is randomly selected by the grading program, the expected output is what it is asking for to get it correct, the actual output is the result I got with the current code I have. I need to edit the code so that the randomly inputted data will deisplay as the expected result based on starting population 100.

The assignment:

Transient Population Populations are effected by the birth and death rate, as well as the number of people who move in and out each year. 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 P

The annual birth rate (as a percentage of the population expressed as a fraction in decimal form)B

The annual death rate (as a percentage of the population expressed as a fraction in decimal form)D

The average annual number of people who have arrived A

The average annual number of people who have moved away M

The number of years to display nYears

Write a function that calculates the size of the population after a year. To calculate the new population after one year, this function should use the formula N = P + BP - DP + A - M where N is the new population size, P is the previous population size, and B, D, A and M are as defined above. The function should return the value computed for N and should receive the values of P, B, D, A and M as parameters.

Prompts And Output Labels. The program first displays the message "This program calculates population change." on a line by itself, followed by these prompts for the inputs described above: "Enter the starting population size: " "Enter the annual birth rate (as % of current population): " "Enter the annual death rate (as % of current population): " "How many individuals move into the area each year? "; "How many individuals leave the area each year? "; "For how many years do you wish to view population changes? " The output of the program starts with a line: Starting population: P (where P is the starting population (surprise)), and then continues with a separate line for each year, each line being of the form: Population at the end of year ||Y is P. (where Y is the year number (1,2,3,...) starting with 1, and where P is the population calculated for that year).

Input Validation. The program should validate all data read. None of the data should be negative, the number of years should not be less than 1 and the initial population should not be less than 2. If an invalid value is read, the program should print an error-specific message on a line by itself, followed by the directive "Please re-enter:" and then input another value-- until a valid value is entered. The error specific messages are: "Starting population must be 2 or more.", "Birth rate percent cannot be negative.", "Death rate percent cannot be negative.", "Arrivals cannot be negative.", "Departures cannot be negative.", and "Years must be one or more.".

My code:

#include

using namespace std;

double newPopulation(int P, double B, double D, int A, int M);//prototyping

int main()

{

int P,A,M,nYears;

double B,D;

cout<<"This program calculates population change."<

cout<<"Enter the starting population size: ";

cin>>P;

while(true){

if(P>=2){break;}

cout<<"Starting population must be 2 or more."<

cout<<"Please re-enter:";

cin>>P;

}

cout<<"Enter the annual birth rate (as % of current population): ";

cin>>B;

while(true){

if(B>0){break;}

cout<<"Birth rate percent cannot be negative."<

cout<<"Please re-enter:";

cin>>B;

}

cout<<"Enter the annual death rate (as % of current population): ";

cin>>D;

while(true){

if(D>0){break;}

cout<<"Death rate percent cannot be negative."<

cout<<"Please re-enter:";

cin>>D;

}

cout<<"How many individuals move into the area each year? ";

cin>>A;

while(true){

if(A>0){break;}

cout<<"Arrivals cannot be negative."<

cout<<"Please re-enter:";

cin>>A;

}

cout<<"How many individuals leave the area each year? ";

cin>>M;

while(true){

if(M>0){break;}

cout<<"Departures cannot be negative."<

cout<<"Please re-enter:";

cin>>M;

}

cout<<"For how many years do you wish to view population changes? ";

cin>>nYears;

while(true){

if(nYears>=1){break;}

cout<<"Years must be one or more."<

cout<<"Please re-enter:";

cin>>nYears;

}

//calling newPopulation method

int count=0;

cout<

cout<<"Starting population: "<

while(count

{

double N;

N=newPopulation(P,B,D,A,M);

P=N;

cout<<"Population at the end of year "<<(count+1)<<" is "<

count++;

}

return 0;

}

//function to get new population

double newPopulation(int P, double B, double D, int A, int M){

double N;

N = (P + B * P - D * P + A - M);

return N;

}

This is the error i get:

Expected Output:

Thisprogramcalculatespopulationchange. Enterthestartingpopulationsize:Entertheannualbirthrate(as%ofcurrentpopulation):Entertheannualdeathrate(as%ofcurrentpopulation):Howmanyindividualsmoveintotheareaeachyear?Howmanyindividualsleavetheareaeachyear?Forhowmanyyearsdoyouwishtoviewpopulationchanges?  Startingpopulation:100  Populationattheendofyear1is103. Populationattheendofyear2is107. Populationattheendofyear3is111. Populationattheendofyear4is114. Populationattheendofyear5is118. Populationattheendofyear6is122. Populationattheendofyear7is125. Populationattheendofyear8is129. Populationattheendofyear9is132. Populationattheendofyear10is136.  

Actual Output:

Thisprogramcalculatespopulationchange. Enterthestartingpopulationsize:Entertheannualbirthrate(as%ofcurrentpopulation):Entertheannualdeathrate(as%ofcurrentpopulation):Howmanyindividualsmoveintotheareaeachyear?Howmanyindividualsleavetheareaeachyear?Forhowmanyyearsdoyouwishtoviewpopulationchanges?  Startingpopulation:100  Populationattheendofyear1is74 Populationattheendofyear2is55 Populationattheendofyear3is42 Populationattheendofyear4is33 Populationattheendofyear5is27 Populationattheendofyear6is22 Populationattheendofyear7is19 Populationattheendofyear8is17 Populationattheendofyear9is15 Populationattheendofyear10is14 

How do i adjust it to match what it is asking for?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Programming The Perl DBI Database Programming With Perl

Authors: Tim Bunce, Alligator Descartes

1st Edition

1565926994, 978-1565926998

More Books

Students also viewed these Databases questions

Question

What are Measures in OLAP Cubes?

Answered: 1 week ago

Question

How do OLAP Databases provide for Drilling Down into data?

Answered: 1 week ago

Question

How are OLAP Cubes different from Production Relational Databases?

Answered: 1 week ago