output in for loop doesn't increment

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 817 Views - Last Post: 17 July 2011 - 01:45 PM Rate Topic: -----

#16 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 207
  • View blog
  • Posts: 550
  • Joined: 12-July 09

Re: output in for loop doesn't increment

Posted 15 July 2011 - 10:31 PM

And I think it's simpler to understand, especially since it's just a small modification from his original code.
Was This Post Helpful? 0
  • +
  • -

#17 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: output in for loop doesn't increment

Posted 15 July 2011 - 10:34 PM

Note that the modification I was talking about was to simply replace both 1s with i.
Was This Post Helpful? 0
  • +
  • -

#18 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 207
  • View blog
  • Posts: 550
  • Joined: 12-July 09

Re: output in for loop doesn't increment

Posted 15 July 2011 - 11:55 PM

That wouldn't work.

Settings:
startSize = 100
annualBirth = 1 (100%)
annualDeath = 0 (0%)


Expected results:
Year 1 population: 100
Year 2 population: 200
Year 3 population: 400


Year 1:
population = startSize
           = 100

Correct!

Year 2:
population = startSize * (i + annualBirth) * (i - annualDeath)
           = 100 * (1 + 1) * (1 - 0)
           = 200

Correct!

Year 3:
population = startSize * (i + annualBirth) * (i - annualDeath)
           = 100 * (2 + 1) * (2 - 0)
           = 600

Wrong!

Now just for fun, let's test my solution.

Year 1:
population = startSize
           = 100

Correct!

Year 2:
population = population * (1 + annualBirth) * (1 - annualDeath)
           = 100 * (1 + 1) * (1 - 0)
           = 200

Correct!

Year 3:
population = population * (1 + annualBirth) * (1 - annualDeath)
           = 200 * (1 + 1) * (1 - 0)
           = 400

Correct!
Was This Post Helpful? 2
  • +
  • -

#19 clickspiker23  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 200
  • Joined: 29-October 10

Re: output in for loop doesn't increment

Posted 17 July 2011 - 01:45 PM

here is what i went with that works. thank you to all who replied especially Hezekiah and PlasticineGuy


    

 double population = startSize;

for (int i = 1; i <= years; i++)
    {


        cout << "Year " << i << "   population: " << population << endl;

        population =  population * (1 + annualBirth - annualDeath);
       

    
    
    }



ill be honest PlasticineGuy i still don't know what you meant when you kept telling me to use 'i' in the calculation. the only thing i could think of was that 'i' was somehow the population i needed to use and then increment it in a calculation but i could never figure out how. the above for loop does what i want it to.

This post has been edited by clickspiker23: 17 July 2011 - 01:52 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2