And I think it's simpler to understand, especially since it's just a small modification from his original code.
18 Replies - 702 Views - Last Post: 17 July 2011 - 01:45 PM
#17
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.
#18
Re: output in for loop doesn't increment
Posted 15 July 2011 - 11:55 PM
That wouldn't work.
Settings:
Expected results:
Year 1:
Correct!
Year 2:
Correct!
Year 3:
Wrong!
Now just for fun, let's test my solution.
Year 1:
Correct!
Year 2:
Correct!
Year 3:
Correct!
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!
#19
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
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.
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
|
|

New Topic/Question
Reply





MultiQuote




|