C++ In a population, the birth rate and death rate are calculated as follows: Bi
ID: 3867597 • Letter: C
Question
C++
In a population, the birth rate and death rate are calculated as follows:
Birth Rate = Number of Births ÷ Population
Death Rate = Number of Deaths ÷ Population
For example, in a population of 100,000 that has 8,000 births and 6,000 deaths per year,
Birth Rate = 8,000 ÷ 100,000 = 0.08
Death Rate = 6,000 ÷ 100,000 = 0.06
Design a Population class that stores a current population, annual number of births, and annual number of deaths for some geographic area. The class should allow these three values to be set in two ways: by passing arguments to a threeparameter constructor when a new Population object is created or by calling the setPopulation, setBirths, and setDeaths class member functions. The class should also have getBirthRate and getDeathRate functions that compute and return the birth and death rates. Your program should properly use pointer data types where appropriate. Write a program that uses the Population class and illustrates its capabilities. Input Validation: If a population figure less than 2 is passed to the class, use a default value of 2. Population must be greater than zero. If a birth or death figure less than 0 is passed to the class, prompt user to type in a positive
Sample output:
Enter total population: 100000
Enter annual number of births: 8000
Enter annual number of deaths: 6000
Population Statistics
Population: 100000
Birth Rate: 0.080
Death Rate: 0.060
Explanation / Answer
#include<iostream>
#include<iomanip>
using namespace std;
class Population {
private:
int curr_population;
int ann_num_births;
int ann_num_deaths;
public:
Population(int a , int b, int c){
curr_population = a;
ann_num_births = b;
ann_num_deaths = c;
}
void setPopulation(int a){
curr_population = a;
}
void setBirths(int a){
ann_num_births = a;
}
void setDeaths(int a){
ann_num_deaths = a;
}
double getDeathRate(){
double a , b;
a = ann_num_deaths;
b = curr_population;
return (a/b);
}
double getBirthRate(){
double a , b;
a = ann_num_births;
b = curr_population;
return (a/b);
}
};
int main(){
int valid;
int pop, nb, nd;
valid = 0;
do {
cout << "Enter total population:";
cin >> pop;
if (pop > 0){
if (pop == 1)
pop = 2;
valid = 1;
}
else
cout << "Please enter a positive value" << endl;
} while (valid == 0);
valid = 0;
do {
cout << "Enter annual number of births:";
cin >> nb;
if (nb > 0)
valid = 1;
else
cout << "Please enter a positive value" << endl;
} while (valid == 0);
valid = 0;
do {
cout << "Enter annual number of deaths:";
cin >> nd;
if (nd > 0)
valid = 1;
else
cout << "Please enter a positive value" << endl;
} while (valid == 0);
Population pt(pop,nb,nd);
cout << "Population Statistics" << endl;
cout << "Popiulation:" << pop << endl;
cout << "Birth Rate:" << setprecision(3) << pt.getBirthRate() << endl;
cout << "Death Rate:" << setprecision(3) << pt.getDeathRate() << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.