Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,578 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,962 people online right now. Registration is fast and FREE... Join Now!




For loop

 
Reply to this topicStart new topic

For loop, For loop

velaphi
28 Sep, 2008 - 02:38 AM
Post #1

New D.I.C Head
*

Joined: 20 Jul, 2008
Posts: 8

In this question you have to write a complete function.
We want to determine what the population of Neverland will be a certain number of years from now, assuming that
Neverland has a steady population growth of 12% per year. You have to write a C++ function population to
calculate this. The function should be a value-returning function of type int and is called in the main function as
follows:
answer = population(popul, nrYears);
where popul indicates the population of Neverland in 2007, and nrYears indicates the number of years from now
(for example 9, if we are interested in the population in 2016). Thus the function should determine what the
population of Neverland will be in 2007 + nrYears.
Assume
• that answer and nrYears have been declared as int,
• that popul has been declared as float, and
• that values have been assigned to both popul and nrYears in the main function.
Write ONLY the complete function population. Use a for loop.

I've tried this, i dont know if is wright.

CODE
population(popul, nrYears)
{
for(int i = 1, i <= nrYears, i++)
{
total += popul;
total++;
cout<<"In "<< i << "the number of population will be "<< total << endl;
}
}

User is offlineProfile CardPM
+Quote Post

Psionics
RE: For Loop
28 Sep, 2008 - 04:13 AM
Post #2

D.I.C Head
Group Icon

Joined: 6 Sep, 2008
Posts: 122



Thanked: 2 times
Dream Kudos: 100
My Contributions
well first of all, it seems you don't understand the basics of function declarations/definitions, and probably for() loops, so I'll comment everything for you:

cpp

// function definitions:
// returnType name( params/arguments )

int population( float popul, int nrYears )
{
// for( initialize; condition; update ) { }
for( int i = 0; i < nrYears; ++i )
{
// equation to update population per year
popul += ( popul * .12 );
}

// return popul so we can assign it in main
return popul;
}


Make sure when you make your condition in your for() loop you use semi-colins (not commas ) to separate the statements.

for() loops run like this: first it checks the initialized variable ( in our case, i ) against the condition ( in our case, i < nrYears ). If this is true, then the body gets executed. After that, the update runs ( in our case, ++i ). So as long as ' i ' is less than the number of years, the loop will continue to run.

In our body, we run the mathematical calculation, and add the new result to the original value of popul. Doing it like this, we're constantly updating the value stored in popul, which allows us to return it to main.

You probably wouldn't print the result out in the function ( that's why you're returning an integer ) but if you do, make sure it's outside the for() loop

Hope this helps smile.gif

This post has been edited by Psionics: 28 Sep, 2008 - 09:08 PM
User is offlineProfile CardPM
+Quote Post

red_4900
RE: For Loop
28 Sep, 2008 - 04:20 AM
Post #3

Code Dreamers
****

Joined: 22 Feb, 2008
Posts: 820



Thanked: 11 times
My Contributions
Mathematically, you're nowhere near the answer. The question says the population growth is a steady 12% per year, but what you're doing is just adding 1 people to the population per year. This is how you add 12% of population per year : popul = (popul/100)*12;.

edited : never mind. psionics already very much gave you the answer.

This post has been edited by red_4900: 28 Sep, 2008 - 04:20 AM
User is online!Profile CardPM
+Quote Post

Psionics
RE: For Loop
28 Sep, 2008 - 04:46 AM
Post #4

D.I.C Head
Group Icon

Joined: 6 Sep, 2008
Posts: 122



Thanked: 2 times
Dream Kudos: 100
My Contributions
lol smile.gif

velaphi - just make sure you understand everything the two of us said in our replies. Functions and loops are two concepts you definitely want to fully understand if you wish to succeed in your programming education smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 12:22AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month