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.