Hello,
For my homework assignment, I'm supposed to write a program that takes the user's input for current population, birth rate, death rate, and number of years and calculates the projected population after n years. We're given these equations that we have to use for our functions:
Population growth = (birth rate - death rate) / 100
Projected population = population + population growth * population
A sample output is given for the assignment:
"Enter the current population: 100000
Enter the birth rate: 5
Enter the death rate: 2
How many years?: 5
After 5 years, the population is projected to be 115928
Another country (y/n)? n"
My problem is with my function for calculating the projected population (called get_projected_population). When I input the same values as the example, it outputs 600000. I tried using a for loop to make iterations for each year, but something isn't working correctly within the function and I'm not sure why. The code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#include <iostream>
#include <cstdlib>
using namespace std;
//function prototype
double population_growth (int, int);
int get_projected_population (double, int, int);
//function logic
double population_growth(int birth_rate, int death_rate) {
double growth_rate;
growth_rate = (birth_rate - death_rate) / 100;
return growth_rate;
}
int get_projected_population(double population_growth, int population, int years) {
int new_projected_population = population;
for (int i = 0; i < years; i++) {
new_projected_population += population + population_growth * population;
}
return new_projected_population;
}
//main function
int main() {
char answer;
do {
int population, birth_rate, death_rate, years, projected_population;
cout << "Enter the current population: ";
cin >> population;
cout << endl;
while (population < 2) {
cout << "Population must be 2 or more, enter again: ";
cin >> population;
cout << endl;
}
cout << "Enter the birth rate: ";
cin >> birth_rate;
cout << endl;
while (birth_rate < 1 || birth_rate > 99) {
cout << "Birth rate must be between 0 and 100, enter again: ";
cin >> birth_rate;
cout << endl;
}
cout << "Enter the death rate: ";
cin >> death_rate;
cout << endl;
while (death_rate < 1 || death_rate > 99) {
cout << "Death rate must be between 0 and 100, enter again: ";
cin >> death_rate;
cout << endl;
}
cout << "How many years?: ";
cin >> years;
cout << endl;
while (years < 1) {
cout << "Number of years must be 1 or more, enter again: ";
cin >> years;
cout << endl;
}
projected_population = get_projected_population(population_growth(birth_rate, death_rate), population, years);
cout << endl << "After " << years << " years, the population is projected to be " << projected_population << endl;
cout << endl << "Another country (Y/N)?: ";
cin >> answer;
cout << endl;
} while (answer == 'Y' || answer == 'y');
return EXIT_SUCCESS;
}
|
I tried looking online for answers, but every similar problem I've found either uses the same type of function and doesn't have an answer, or uses a different equation. Any help with this would be greatly appreciated, thanks in advance.