Hey guys, I'm stuck again.

The program is supposed to get the estimated population after n years using the equation P + (BP/100)-(DP/100) and the growth rate using B-D.

Here is what I have so far, but I can see I'm going to be getting stuck.
How can I set my parameters for estimatedPopulation to grab the values from growthRate in addition to the current population and number of years?

#include <iostream>
#include <string>

using namespace std ;

int main()
{
    int x ;
    int population ;

    population = estimatedPopulation(growthRate()) ;

    cout << "Estimated population after " << n << " years:  " << population << endl ;

    return 0 ;
}

int growthRate ()
{
    int birthRate ;
    int deathRate ;
    int growthRate ;

    cout << "Enter the birth and death rates per year, seperated by a space:  " << flush ;
    cin >> birthRate, deathRate ;

    if ( ( birthRate < 0 ) || ( deathRate < 0 ) )
    {
        cout << "You can't have negative death/birth rates, dumbie!" << endl ;
        return 0 ;
    }

    growthRate = birthRate - deathRate ;
    return growthRate ;
}

int estimatedPopulation ()
{
    int cPopulation ;
    int population ;
    int n ;

    cout << "Enter the current population:  " << flush ;
    cin >> cPopulation ;
    cout << "Enter the number of years to calculate:  " << flush ;
    cin >> n ;

}

I rewrote my a lot of my code. Here is the revised version.

I don't understand the equations they're giving me. I'm also getting an error that says

error C2664: 'estimatedPopulation' : cannot convert parameter 1 from 'int (__cdecl *)(int,int)' to 'int'

at line 28

What does this mean?
I stopped after the growthRate function, because I can't seem to get that one working let alone the other. I'm not sure how input the equations the book is giving; could someone explain what the equations are saying?

#include <iostream>
#include <string>

using namespace std ;

int growthRate ( int, int ) ;
int estimatedPopulation ( int, int, int ) ;

int main()
{
    int x ;
    int population ;
    int cPopulation ;
    int birthRate ;
    int deathRate ;
    int n ;

    cout << "Enter the current population:  " << flush ;
    cin >> cPopulation ;
    cout << "Enter the birth rate:  " << flush ;
    cin >> birthRate ;
    cout << "Enter the death rate:  " << flush ;
    cin >> deathRate ;
    cout << "Enter the number of years to calculate:  " << flush ;
    cin >> n ;

    x = growthRate( birthRate , deathRate ) ;
    population = estimatedPopulation ( growthRate , cPopulation, n ) ;

    cout << "Estimated population after " << n << " years:  " << population << endl ;

    return 0 ;
}

int growthRate (int birthRate, int deathRate)
{
    int growthRate ;

    if ( ( birthRate < 0 ) || ( deathRate < 0 ) )
    {
        cout << "You can't have negative death/birth rates, dumbie!" << endl ;
        return 0 ;
    }

    growthRate = birthRate - deathRate ;
    return growthRate ;
}

int estimatedPopulation (int growthRate, int cPopulation, int n)
{
    int population ;

}

Line 27 and 28:

x = growthRate( birthRate , deathRate ) ;
population = estimatedPopulation ( growthRate , cPopulation, n ) ;

SHould either be:

x = growthRate( birthRate , deathRate ) ;
population = estimatedPopulation ( x, cPopulation, n ) ;

OR

//x = growthRate( birthRate , deathRate ) ;
population = estimatedPopulation ( growthRate(birthRate , deathRate), cPopulation, n ) ;
commented: thanks for the help! -duki +1

I think the error "error C2664: 'estimatedPopulation' : cannot convert parameter 1 from 'int (__cdecl *)(int,int)' to 'int'" is clear enough though.. just look at the first param..

<li class="li1">int estimatedPopulation (int growthRate, int cPopulation, int n)
<li class="li2">{
<li class="li1"> int population ;
<li class="li1">
<li class="li1">in your funstion declaration, you are asking for an int parameter:
<li class="li1">int estimatedPopulation ( int, int, int ) ;
<li class="li1">but you are give a funtion parameter which is
growthRate instead of int variable

Well the good news is the program is compiling, thanks to the help I've received. Now I have a math error somewhere >.<. Could someone give me a hand?

Sample Output:
Enter the current population: 1000
Enter the birth rate: 100
Enter the death rate: 50
Enter the number of years to calculate: 2
Estimated population after 2 years: 2500
Press any key to continue . . .

/*****************************************************
* COSC 230 - Structured Programming
* Chapter 6:  Programming Exercise 7
* Mar. 20, 2007
*
* Discription:
*    Determine population growth rate
*    Determine population after 'n' years
******************************************************/

//headerfiles
#include <iostream>
#include <string>

using namespace std ;

//function prototypes
int growthRate ( int, int ) ;
int estimatedPopulation ( int, int, int ) ;

int main()
{
    int population ;
    int cPopulation ;
    int birthRate ;
    int deathRate ;
    int n ;

    cout << "Enter the current population:  " << flush ;
    cin >> cPopulation ;
    cout << "Enter the birth rate:  " << flush ;
    cin >> birthRate ;
    cout << "Enter the death rate:  " << flush ;
    cin >> deathRate ;
    cout << "Enter the number of years to calculate:  " << flush ;
    cin >> n ;

    /*x = growthRate( birthRate , deathRate ) ;*/
    population = estimatedPopulation ( growthRate(birthRate , deathRate), cPopulation, n ) ;

    cout << "Estimated population after " << n << " years:  " << population << endl ;

    return 0 ;
} //end 'main'

int growthRate (int birthRate, int deathRate)
{
    int growthRate ;

    if ( ( birthRate < 0 ) || ( deathRate < 0 ) )
    {
        cout << "You can't have negative death/birth rates, dumbie!" << endl ;
        return 0 ;
    }

    growthRate = birthRate - deathRate ;
    return growthRate ;
} //end 'growthRate'

int estimatedPopulation (int growthRate, int cPopulation, int n)
{
    int x ;

    x = ( cPopulation + growthRate * cPopulation / 200 ) * n ;
    return x ;

} //end 'estimatedPopulation'

Wouldn't the formula for calculating the population be:

( (population + growthRate) / population ) * population

Because let's say you have 100 people, 100 birthrate, and 50 deathrate. 100-50 = 50, so that's your growth rate. Population + growthRate would be 100 + 50, so 150. Divide that by your population, and you have 1.5.

Now... when you multiply your population, 100, by 1.5, it increases by 50. Since the population would compound, you need to create a loop for the number of years.

Or did I totally miss it?

hey that sounds sweet to me... i'll try it and let you know. I bet the book didn't even account for the compounding growth.


The books equation: P + (B*P/100 - D*P/100)

Question for you (I'm not terribly good at math, so excuse this if it's dumb): why did you calculate the difference of birth and death rates when the formula didn't need that? This is how I would have written the formula from the book:

(I'm assuming that B=birthrate and D=deathrate)

int
estimatePopulation(int birthRate, int deathRate, int population, int years) {
    
    population = (population + ( birthRate * population / 100) 
                                 - ( deathRate * population / 100) ) * years;
    
    return population;
}

Not sure what you're asking, but if it is about the growthRate, that was another part of the problem. The equation they gave for that was B-D

Not sure what you're asking, but if it is about the growthRate, that was another part of the problem. The equation they gave for that was B-D

So why did you combine the 2 into one formula? From what I understand, the first formula to calculate the population only needs birthrates and deathrates, not the difference.

First off as mentioned it was your formula:
x = ( cPopulation + growthRate * cPopulation / 200 ) * n

using population of 1000, birthR 100, deathR 50, years 2

x = (1000 + (50*1000/200))*2 or x = 1250*2

now first off you added your current population * number of years which gave you the extra 1000. Secondly you aren't compounding, as was also mentioned.

Third of all your birthrate and deathrate are confusing and may throw off figures, birth-deathrate can be done in a number of methods.
1: total births, total deaths
2: ratio of births to deaths
3: % of population in births and deaths
based on your books /100 i am guessing they want the third method which means you should end up with something like:


// for (int a = 0; a<n; a++) {
// x=((cpop+x)*birthR/100);
// }
// pop = cpop+x;

and the book equation was for a single year so it didn't have to deal with compounding the population, which you do.

Here it's updated.. (See comments in code)

/*****************************************************
* COSC 230 - Structured Programming
* Chapter 6:  Programming Exercise 7
* Mar. 20, 2007
*
* Discription:
*    Determine population growth rate
*    Determine population after 'n' years
******************************************************/

//headerfiles
#include <iostream>
#include <string>

using namespace std ;

//function prototypes
int growthRate ( int, int ) ;
int estimatedPopulation ( int, int, int ) ;

int main()
{
    int population ;
    int cPopulation ;
    int birthRate ;
    int deathRate ;
    int n ;

    cout << "Enter the current population:  " << flush ;
    cin >> cPopulation ;
    if( cPopulation < 2 )
    {
        cout << "You have no hope.. !" << endl
            << "Current population must be at least 2 for natural human evolation.. :)" << endl ;
        return 1 ;
    }
    cout << "Enter the birth rate:  " << flush ;
    cin >> birthRate ;
    cout << "Enter the death rate:  " << flush ;
    cin >> deathRate ;
    cout << "Enter the number of years to calculate:  " << flush ;
    cin >> n ;
    if( ( birthRate <= 0 ) ||
        ( (deathRate <= 0) || (deathRate > 100) ) ||
        ( n <= 0 ) )
    {
        cout << "Birth rate, Death rate and Number of years must be greater than 0. And Death rate can't be greater than 100." << endl ;
        return 1 ;
    }

    /*
        NEW_P = P + (B*P/100 - D*P/100)
        ==> NEW_P = P + ((B - D) * (P/100))
        ==> NEW_P = P + (G * (P/100))
            where G is growthrate viz = B-D

        PS:
        B, D and G are % rates meaning per 100. So we divide by 100.
        Hope that's clear..
    */
    /*x = growthRate( birthRate , deathRate ) ;*/
    int growth_rate = birthRate - deathRate ;
    population = cPopulation + ( growth_rate * (cPopulation/100) ) ;
    //OR
    //population = cPopulation + ( (birthRate - deathRate) * (cPopulation/100) ) ;

    cout << "Estimated population after " << n << " years:  " << population << endl ;

    return 0 ;
} //end 'main'

New output:
Enter the current population: 1000
Enter the birth rate: 100
Enter the death rate: 50
Enter the number of years to calculate: 2
Estimated population after 2 years: 1500
Press any key to continue

hey that sounds sweet to me... i'll try it and let you know. I bet the book didn't even account for the compounding growth.


The books equation: P + (B*P/100 - D*P/100)

Please note that neither my nor your code take care of compounding. If that's needed, a small loop will solve the problem.

Great, thanks everyone. Program is working fine now.

Finalized code:

/*****************************************************
* COSC 230 - Structured Programming
* Chapter 6:  Programming Exercise 7
* Mar. 20, 2007
*
* Discription:
*    Determine population growth rate
*    Determine population after 'n' years
******************************************************/

//headerfiles
#include <iostream>
#include <string>

using namespace std ;

//function prototypes
double growthRate ( double, double ) ;
int estimatedPopulation ( double, int, int ) ;

int main()
{
    int population ;
    int cPopulation ;
    double birthRate ;
    double deathRate ;
    int n ;

    cout << "Enter the current population:  " << flush ;
    cin >> cPopulation ;
    if (cPopulation < 2)
    {
        cout << "You have no hope.  Current population must be at least two for natural human evolution. " << endl ;
        return 1 ;
    }
    cout << "Enter the birth rate:  " << flush ;
    cin >> birthRate ;
    cout << "Enter the death rate:  " << flush ;
    cin >> deathRate ;
    cout << "Enter the number of years to calculate:  " << flush ;
    cin >> n ;
    cout << "Computing..." << endl << endl ;

    if ( (birthRate < 0) || (deathRate < 0) || (deathRate > 1) || (n < 0) )
    {
        cout << "Birth rate, death rate, and years must be greater than 0.  Death rate can not exceed 100%." << endl << endl ;
        return 1 ;
    }

    /*x = growthRate( birthRate , deathRate ) ;*/
    population = estimatedPopulation ( growthRate(birthRate , deathRate), cPopulation, n ) ;

    cout << "Estimated population after " << n << " years:  " << population << endl ;

    return 0 ;
} //end 'main'

double growthRate (double birthRate, double deathRate)
{
    double growthRate ;

    growthRate = birthRate - deathRate ;
    return growthRate ;
} //end 'growthRate'

int estimatedPopulation (double growthRate, int cPopulation, int n)
{
    int x ;
    int population = cPopulation ;

    for ( int a = 0 ; a < n ; a++ )
        population = ( (population * growthRate) + population ) ;

    return population ;

} //end 'estimatedPopulation'
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.