calculate population growth

Hi there,

I wrote a little program for calculation of population after n years...

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
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    bool stop=false;
    while( !stop )
    {
        // parameters
        int nYears=-1;
        float birthRateAsPercentages=-1;
        float deathRateAsPercentages=-1;
        float growthRate=-1;
        float populationAfter0Years=-1;
        float populationAfterNYears=-1;

        // read parameters
        while( populationAfter0Years<0 ){
            cout << "current population ";
            cin >> populationAfter0Years;
        }
        while( birthRateAsPercentages<0 || birthRateAsPercentages>100){
            cout << "birth rate (% per year) ";
            cin >> birthRateAsPercentages;
        }
        while( deathRateAsPercentages<0 || deathRateAsPercentages>100 ){
            cout << "death rate (% per year) ";
            cin >> deathRateAsPercentages;
        }
        while( nYears<0 ){
            cout << "duration (years) ";
            cin >> nYears;
        }

        // calculate result
        growthRate = (birthRateAsPercentages - deathRateAsPercentages) / 100;
        populationAfterNYears = populationAfter0Years;
        for(int i=0; i<nYears; i++)
            populationAfterNYears += populationAfterNYears * growthRate;

        // show result
        cout << "population after " << nYears << " years is " << populationAfterNYears;

        // repeat?
        char again;
        cout << "\nagain? ";
        cin >> again;
        if( 'y' != again )
            stop=true;
    }
    return 0;
}


hope u like it!? :)
Last edited on
Topic archived. No new replies allowed.