darko
darko

Reputation: 2458

calculations in function seem to be ignored, same number returned always

growthRate function: (as of 8:00 PM 12/11/2010)

    #include "header.h"

float growthRate (float birthRate, float deathRate)     
{   
    float growthrt;  
    growthrt =  (birthRate) - (deathRate);
    cout << growthrt << endl; 
    return growthrt;

}

estimatedPopulation function: (as of 8:00 PM 12/11/2010)

#include "header.h"

float estimatedPopulation (float currentPopulation, float years, float birthRate, float deathRate)
{
    int x;
    float newPopulation;
    for (x = 0; x < years; x++)
    {
    newPopulation = currentPopulation + currentPopulation * (growthRate (birthRate, deathRate) / 100);
    currentPopulation  = newPopulation;
    cout << newPopulation << endl;
    }

    return newPopulation;
}

output function:(as of 8:00 PM 12/11/2010)

#include "header.h"

void output (float currentPopulation, float years)

{     
     cout <<  estimatedPopulation (currentPopulation, years) << endl;
}     

main function:(as of 8:00 PM 12/11/2010)

#include "header.h"

int main ()
{

    float currentPopulation, birthRate, deathRate, years;

    char response; 


     do //main loop
      { 
           input (currentPopulation, birthRate, deathRate, years);
           growthRate (birthRate, deathRate);
           estimatedPopulation (currentPopulation, years, birthRate, deathRate);
           output (currentPopulation, years, birthRate, deathRate);
           cout << "\n Would you like another population estimation? (y,n) ";
           cin >> response;
      }          
    while (response == 'Y' || response == 'y');

    myLabel ("5-19", "12/09/2010");   

    system ("Pause");

    return 0;

}    

edit (8:00 PM 12/11/2010)

Solved! parameters, parameters, parameters! thanks for the input everyone.

Upvotes: 0

Views: 129

Answers (6)

Falmarri
Falmarri

Reputation: 48577

You don't see a problem with this code?

void output ()

{
     float currentPopulation, years;

     cout <<  estimatedPopulation (currentPopulation, years) << endl;
}     

If you really can't see why this is wrong, then you should start off with MUCH simpler programs.

Upvotes: 0

sje397
sje397

Reputation: 41822

You have two main problems.

The first problem is that your return value from estimatedPopulation is an int. So when the result of your expression is evaluated, it is converted to an int, which will round it down. Unless the result of currentPopulation * growthrt / 100 is greater than one, you'll never see a change. Same issue with your last example I think.

The other problem is that you keep assigning the same value to the newPopulation variable. You probably want to adjust currentPopulation within the loop.

@Matt: it looks like you're not using your return values from your function, e.g.:

something = estimatedPopulation(currentPopulation, years);

(By the way - that function is supposed to take 4 parameters???)

And be aware that when you pass in an 'int' that variable within the function is local to the function - modifying it will have no effect on the variable of the same name outside the function. It looks very much like this is what you're expecting to happen. If you do want to modify the value you pass in, you might try something like:

void estimatedPopulation(int &newPopulation....) {

This passes the variable in by reference so that you can actually modify it within the function.

Upvotes: 3

Tristan
Tristan

Reputation: 916

There is nothing updating newPopulation or currentPopulation as the loop iterates, so newPopulation is just getting overwritten several times with the same value. I imagine at some point in that loop you want a line like currentPopulation = newPopulation;

EDITED TO ADD:

Now that you added your main function: You are treating all of your function calls as though they were of type void. That is, you aren't actually using any of the return values from estimatedPopulation or growthRate, so I am honestly surprise you are getting anything but garbage values printed in your output function, as its argument is never set.

Upvotes: 2

Ben Voigt
Ben Voigt

Reputation: 283634

In addition to what everyone else said, it looks like growthrt is being divided by 100 twice, once inside function growthRate and again inside your loop in estimatedPopulation.

Another thing you're going to run into as soon as you get things updating is that the loop runs years+1 times, which may be more than you want.

Upvotes: 1

javs
javs

Reputation: 808

You are passing newPopulation as a parameter but by value, not by reference, or a pointer. So its not updated by your code.

You are also not touching that variable, so it will just do one iteration.

Upvotes: 1

paul23
paul23

Reputation: 9435

I think this is just a case of rounding with ints, say "currentpopulation" is 100, "growthrt" is 0.1. then: the sum becomes: 100 + 100 * 0.1 / 100 = 100.1; - This then however is set as the next "newPopulation". Yet since new population is an integer, it is rounded down to 100 again!

Better change "newPopulation" to a double/floating point number (and maybe at the end of the function round it down again)

Upvotes: 0

Related Questions