## Trouble with loops (C++)

Moderators: phlip, Moderators General, Prelates

Nimz
Posts: 580
Joined: Thu Aug 02, 2007 9:49 am UTC
Location: the origin

### Trouble with loops (C++)

I am nesting loops within loops, but for some reason the inner loop only executes the first time the outer loop executes, even though the instructions in the outer loop after the inner loop reset the parameters used by the inner loop. Specifically:

Code: Select all

`for(r=4; r<=(1<<(n-1)); r++)  // I'm using << to shift the 1 bit left as a quick way to get 2^(n-1){   while(pos>1)   {      x[pos]++;   // x[1<<n] is an array of integers.  x[0] and x[1] are supposed to be fixed at 0 and 1 respectively.  x[2] was previously initialised as 1 so that it's 2 when the loop first runs.  pos is initialised as 2.      while(pos<r-1)      {         pos++;         x[pos]=x[pos-1]+1;   // Initializing the array / resetting the array after the maximum value has been achieved by a particular x[k].      }      STUFF  // some functions independent of the loops are called.  STUFF works (three bug fixes later).      while(x[pos]<((1<<(n))-1))   // This should be the same if I replaced x[pos] with x[r-1].      {         x[pos]++;         STUFF      }      while(x[pos]=((1<<(n))-r+pos))      {         pos--;   // The maximum allowed value for x[pos] has been reached, so we go to the previous pos to see if that has also reached the maximum.  x[1] should always be 1, so this *should* terminate when pos = 1, which *should* terminate the pos>1 loop.      }   }}`

For n=4 this should have x take on the values [0 1 2 3 0 0 0 0] ... [0 1 2 15 0 0 0 0] [0 1 3 4 0 0 0 0] ... [0 1 3 15 0 0 0 0] ... ... [0 1 14 15 0 0 0 0] [0 1 2 3 4 0 0 0] ... ... ... [0 1 10 11 12 13 14 15].
Instead, x only takes on the values [0 1 2 3 0 0 0 0] ... [0 1 2 15 0 0 0 0]. By placing some troubleshooting couts I found that the variable pos started taking on negative values (indeed, going to -4) before the program run finished. Some funny business is going on that I haven't been able to dope out yet. Any help would be greatly appreciated.
LOWA

Ended
Posts: 1459
Joined: Fri Apr 20, 2007 3:27 pm UTC
Location: The Tower of Flints. (Also known as: England.)

### Re: Trouble with loops (C++)

The thing that jumps out is

Code: Select all

`while(x[pos]=((1<<(n))-r+pos))`

Do you want == here?
Generally I try to make myself do things I instinctively avoid, in case they are awesome.
-dubsola

Nimz
Posts: 580
Joined: Thu Aug 02, 2007 9:49 am UTC
Location: the origin

### Re: Trouble with loops (C++)

Yes. Yes, I do.
It still doesn't quite do what I want with the proper number of ='s in that while, but it's enough that I can get by. What it's doing now is running through all the values of x properly for r=4, but it's not doing anything when r=5.

Oh, snap. I don't have anything that resets pos or x[2] once that while terminates. Throwing in

Code: Select all

`   x[2]=1;   pos=2;`
at the end of the outer loop fixes that. Thank you very much for pointing out my n00bly coding mistake.
LOWA

Infernalis
Posts: 68
Joined: Mon Nov 10, 2008 10:13 pm UTC
Location: Cube farm

### Re: Trouble with loops (C++)

It's entirely possible for me to be wrong, I haven't had to deal with C++ in a while so I'm rusty on the sntax. But shouldn't the condition for the for inner loop:
Nimz wrote:

Code: Select all

`      while(pos<r-1)      {...}`

be this?:

Code: Select all

`      while(pos<(r-1))      {...}`

Nimz
Posts: 580
Joined: Thu Aug 02, 2007 9:49 am UTC
Location: the origin

### Re: Trouble with loops (C++)

The extra parentheses wouldn't hurt. But it doesn't seem like they're necessary, as my code works and does exactly what it's supposed to do now.
LOWA

PM 2Ring
Posts: 3713
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

### Re: Trouble with loops (C++)

Nimz wrote:The extra parentheses wouldn't hurt. But it doesn't seem like they're necessary,

The comparison operators have a lower precendence than the arithmetic operators, so extra parentheses are not necessary. Too many parentheses make code look cluttered, but IMHO it's better to use too many than not enough.
Nimz wrote:my code works and does exactly what it's supposed to do now.

Yay!

(I almost didn't read this thread because I don't do C++, but I've been using C since 1980).

### Who is online

Users browsing this forum: No registered users and 3 guests