Problem with function, population growth after n years

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.
Last edited on
(birth_rate - death_rate) / 100; integer division, discards fractional part (your function will always return 0). Fix: make one of the values a double. Easiest way is to change literal: (birth_rate - death_rate) / 100.0;

Additionally you should either use
1
2
3
population = population + population_growth * population;
//or
population += population_growth * population
But not both at the same time. (you do not need a new_projected_population variable at all, just use population)
Last edited on
Ok, I made your changes and there's definitely an improvement, but my answer is off by 2. The new code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
double population_growth(int birth_rate, double death_rate) {
    
    double growth_rate;
    growth_rate = (birth_rate - death_rate) / 100.0;
    
    return growth_rate;
}

int get_projected_population(double population_growth, int population, int years) {
    
    for (int i = 0; i < years; i++) {
        
        population = population + population_growth * population;
        
    }
    
    return population;
    
}


When using the example values, it gives me 115926 instead of 115928. We had another example given where the population is 1000, birth rate is 4, death rate is 1, and number of years is 10 with the answer being 1344. When I input those values, I get 1336, meaning I've got an arithmetic problem somewhere...
Well, I noticed that too, and I had a long time figuring out exact code needed. Personally I would just make it as precise as possible and then argue with teacher, but here my line of thought and final results:
1) There is a truncation on each iteration. Lets make it exact (truncating projected population down — 10.7 of projected population will be truncated to 10):
1
2
3
4
5
6
int get_projected_population(double population_growth, double population, int years) 
{
    for (int i = 0; i < years; i++)
        population = population + population_growth * population;
    return population; //Truncation here
}
It was still off.

2) Well, I though, maybe he want to round mathematically?
1
2
3
4
5
6
int get_projected_population(double population_growth, double population, int years) 
{
    for (int i = 0; i < years; i++)
        population = population + population_growth * population;
    return std::round(population);
}
It was still off.
Note that 1 is most precise and reflect reality (correct answer, calculated by hand is ≈115927,4 and ≈1343,9 respectively, and fractional people are usually rounded down)
After some fiddling, I found out (and fixed it slightly with your new input) how to make exactly your numbers:
1
2
3
4
5
6
int get_projected_population(double population_growth, double population, int years) 
{
    for (int i = 0; i < years; ++i)
        population += population_growth * population;
    return std::ceil(population);
}


You have either omitted important part you your assigment, or that important part was not mentioned. Notice that std::ceil resides in <cmath> header, so you would need that.
Last edited on
Ah, yep, that did it. Thanks so much for your help. That important part wasn't mentioned anywhere in the assignment.
Topic archived. No new replies allowed.