Question

P3.4 Population. In a population, the birth rate is the percentage increase of the population due to births, and the death ra

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the program in C++ for the requirements mentioned in the question. It contains a function population_change() which calculates the population for every next year based on the valid birth rate and death rate. Population is rounded off as shown in the question to the closest intger using the round() function in C++. All the invalid input cases are handled. Sample output is attached at the end showing the behavior of program on all valid inputs and on one of the invalid input given by the user.

#include <iostream>
#include <cmath>
using namespace std;

// function calculating population change every year.
// round off the population to nearest integer.
void population_change(double currpop, double birth, double death, int years){
    
    for(int i = 1; i <= years; i++){
        currpop = currpop + (birth*currpop)/100 - (death*currpop)/100;
        currpop = round(currpop);
        cout<<"Population at the end of year "<<i<<" is "<<currpop<<endl;
    }
    
}

int main()
{
    cout<<"This program calculates population change.\n";
    double pop;
    double brate, drate;
    int nyears;
    cout<<"enter the starting size of the population: ";
    cin>>pop;
    // handling invalid population case.
    if(pop < 2){
        cout<<"Population size less than 2 which is invalid.";
        return 0;
    }
    
    cout<<"enter the annual birth rate (as % of current population): ";
    cin>>brate;
    // handling invalid birth rate case.
    if(brate < 0){
        cout<<"birth rate is negative which is invalid.";
        return 0;
    }
    
    
    cout<<"enter the annual death rate (as % of current population): ";
    cin>>drate;
    // handling invalid death rate case.
    if(drate < 0){
        cout<<"death rate is negative which is invalid.";
        return 0;
    }
    
    cout<<"For how many years do you wish to view population change? ";
    cin>>nyears;
    // handling invalid number of years case.
    if(nyears < 1){
        cout<<"number of years are less than 1 which is invalid.";
        return 0;
    }
    
    
    cout<<endl;
    cout<<"starting population: "<<pop<<endl<<endl;

    // calling the function to calculate population change.
    population_change(pop, brate, drate, nyears);
    
    return 0;
}

Sample Output:-

Output when all the values are valid.

This program calculates population change. enter the starting size of the population: 1000 enter the annual birth rate (as &

Output when population is invalid.

This program calculates population change. enter the starting size of the population: 1 Population size less than 2 which is

Output when birth rate is negative.

This program calculates population change. enter the starting size of the population: 1000 enter the annual birth rate (as %

Output when death rate is negative.

This program calculates population change. enter the starting size of the population: 1000 enter the annual birth rate (as %

Output when years are less than 1.

This program calculates population change. enter the starting size of the population: 1000 enter the annual birth rate (as ;

Add a comment
Know the answer?
Add Answer to:
P3.4 Population. In a population, the birth rate is the percentage increase of the population due...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++ Transient Population Populations are effected by the birth and death rate, as well as...

    In C++ 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...

  • 17. Prime Numbers A prime number is a number that is only evenly divisible by itself...

    17. Prime Numbers A prime number is a number that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided by 1 and S. The number 6, how- ever, is not prime because it can be divided evenly by 1, 2, 3, and 6. Write a Boolean function named is_prime which takes an integer as an argument and returns true if the argument is a prime number, or...

  • a prime number is a number that is only evenly divisible by itself and 1. for...

    a prime number is a number that is only evenly divisible by itself and 1. for example the number 5 is prime because it can only be evenly divided by 1 and 5 the number 6 however is not prime because it can be divided evenly by 1,2,3 and 6. write a function name isPrime which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. use this functuion in a...

  • Country A 144 Country B 82 43 8 18 10 Population (millions) Crude birth rate (number...

    Country A 144 Country B 82 43 8 18 10 Population (millions) Crude birth rate (number of live births per 1,000 people per year) Crude death rate (number of deaths per 1,000 people per year) Infant mortality rate (number of babies per 1,000 bom who die in first year of life) Total fertility rate (average number of children born to women during their childbearing years) % of population under 15 years old % of population older than 65 years Average...

  • 5.11: Population Write a program that will predict the size of a population of organisms. The...

    5.11: Population Write a program that will predict the size of a population of organisms. The program should ask the user for the starting number of organisms, their average daily population increase (as a percentage, expressed as a fraction in decimal form: for example 0.052 would mean a 5.2% increase each day), and the number of days they will multiply. A loop should display the size of the population for each day. Input Validation.Do not accept a number less than...

  • Please Write CLEARLY Create a program that uses functions and arrays that performs the following: Creates...

    Please Write CLEARLY Create a program that uses functions and arrays that performs the following: Creates a report to display to the user containing the following information: o Accept user input for a starting number o determine whether or not the entered number is prime or not o find the next 20 prime numbers after your selected numbers o display your results This can be created either using the console application as we've done in class or as a windows...

  • Populations grow when the number of births in a population is greater than the number of...

    Populations grow when the number of births in a population is greater than the number of deaths in the same population and there is no net movement of individuals into or out of the population. This is to say that all else being equal, populations grow when the birth rate exceeds the death rate and shrink when death rates exceed birth rates. When the number of births is equal to the number of deaths in a population, and again, there...

  • 1. Single Species Growth Consider a single population where the per capita birth rate declines as...

    1. Single Species Growth Consider a single population where the per capita birth rate declines as the population size grows. Let N(t) be the population size at time t. Consider the following assumptions: (A1) The environment in which the species lives (including the climate, other species and the availability of resources like food, etc.) remains constant. (A2) The per capita birth rate is for some b>0. (A3) The per capita death rate is a constant d > 0. Note: This...

  • Write a program that will predict the size of a population of organisms. The program should ask t...

    Write a program that will predict the size of a population of organisms. The program should ask the user for the starting number of organisms, their average daily population increase (as a percentage), and the number of days they will multiply. A loop should display the size of the population for each day. Input Validation: Do not accept a number less than 2 for the starting size of the population. "The starting number of organisms must be at least 2."...

  • this is c++ and please let the code works on vs2017. the output should be same...

    this is c++ and please let the code works on vs2017. the output should be same as the question. mes A. Hotz Programming Challenge: Prime qumber Array Values from Canvas: ICE12A Please name your source file: PC7101.CPP A prime number is a number that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided by 1 and 5 The number 6, however, is not prime because it can...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT