# For loop

Page 1 of 1

## 3 Replies - 1994 Views - Last Post: 28 September 2008 - 05:46 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=65346&amp;s=4fbe84a29cd73de327aeaa6db4004a49&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 velaphi

Reputation: 1
• Posts: 55
• Joined: 20-July 08

# For loop

Posted 28 September 2008 - 03:38 AM

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:
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.

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

Is This A Good Question/Topic? 0

## Replies To: For loop

### #2 Psionics

Reputation: 13
• Posts: 158
• Joined: 06-September 08

## Re: For loop

Posted 28 September 2008 - 05:13 AM

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:

```// 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

This post has been edited by Psionics: 28 September 2008 - 10:08 PM

### #3 red_4900

• Code T(h)inkers

Reputation: 21
• Posts: 1,120
• Joined: 22-February 08

## Re: For loop

Posted 28 September 2008 - 05:20 AM

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 September 2008 - 05:20 AM

### #4 Psionics

Reputation: 13
• Posts: 158
• Joined: 06-September 08

## Re: For loop

Posted 28 September 2008 - 05:46 AM

lol

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