Population Estimation

Hi!
I'm writing a program that's supposed to estimate the population (originally 26 000) of a town, in which there are 0.7 % of the total population born every year, and 0.6 % deceased every year. There are also 300 people who immigrate to the town every year, and 325 people who emigrate every year. This should give a growth in population of 1 person every year, but that isn't what my program outputs! It outputs no change in population at all... Can you help me with getting it to work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cstdlib>
using namespace std;

int main(){
    int year = 0, inhabitants = 26000, immigrants = 300, emigrants = 325;
    double born = 1.007, deceased = 1.006;
    while(year <= 0){
        system("CLS"); // Sorry for using this by the way, I know I shouldn't...
        cout << "Enter how many years have passed since the measurements started (2000): "; cin >> year;
    }
    for(int x=0;x<year;x++){
        inhabitants += born*inhabitants - deceased*inhabitants + immigrants - emigrants;
    }
    cout << "The number of inhabitants is: " << inhabitants;
    return(0);
}
Last edited on
You are experiencing a rounding error that is typical of floating point calculations.

You might do: inhabitants += (born-deceased)* inhabitants + immigrants - emigrants;

or even:
1
2
3
4
5
6
7
8
    // ...
    double netBirthRate = born - deceased ;
    double netImmigration = immigrants - emigrants ;

    for ( int x=0; x<year; ++x)
        inhabitants += netBirthRate * inhabitants + netImmigration ;

    // ... 

Last edited on
Goran, you have to use the pow() function to find the yearly population increase. It's just like compound interest, only with people!

I have amalgamated your figures for simplicity. eg 300 people arriving and 325 leaving means a net loss of 25.
Also the net population growth from births and deaths is 1.001.

Here is the simplified code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
     int years;
     float inhabitants;
   
     cout << "Enter how many years have passed since records started in 2000: ";
     cin >> years;

     inhabitants = 26000*pow(1.001,years) - 25*years;
     cout << "\nThe number of inhabitants is: " << inhabitants;

     cin.get();
     cin.get();
     return(0);
}


You can add now add back the various detailed additions and subtractions if you need to. Hope this helps. Donnie

EDIT. I didn't see Cire's post above until after I sent this. It is far more sophisticated! But I guess it shows there are different ways of doing things.
Last edited on
Thanks for the help! If I understood you correctly cire, the program is rounding +1 to 0. Am I right?
Last edited on
Wow Donnie! Your way of doing it was much simpler, I hadn't thought about doing it that way. Thanks :) By the way, why did you write "cin.get();" at the end?
Last edited on
Goran, putting
cin.get();
twice at the end of the code is the simplest way I know to stop the console screen from immediately disappearing once the program is run outside Code::Blocks.

I realize there are many other ways of doing this, as detailed in the second beginners' post, but I find this easy to remember! Donnie
Topic archived. No new replies allowed.