I'm having a pretty weird logic error. It seems that this code is always ending my arrays with 3. It should (being a recursive function) iterate through 0, 1, 2, 3. My test array is of length 4 but I am completely missing the reason. Can anyone else see the error?

work(array, where)
{
        if(where <= array.length - 1)
        {
                for(int i = 0; i <= array.length - 1; i++)
                {
                        array[where] = i;
	                work(array, where + 1);
                }
        }
}

NOTE: Where is initially 0

Recommended Answers

All 5 Replies

You're not describing what you expect the code to do. I would expect the code to fill the array with threes.

Sorry! It's supposed to recursively fill an array with all permutations of 0-3. I have other code that handles different situations but this is supposed to produce areas such as:

0 0 0 0
0 0 0 1
0 0 0 2
0 0 0 3
0 0 1 0
0 0 1 1
...
up to

3 3 3 3

It currently produces many permutations but they all end with a 3 in address 3.

Well from what I see, it walks through the permutations but eventually overwrites the array with 3 3 3 3. There is no way anything will be able to see the array in a different state.

Right I've only included the bit of code that writes the arrays... there is other code that handles each array separately. But for some reason when I print out the results the first 3 elements of the array will be changed but the last one will be 3.

Sorry I found out the method is working and another method is distorting the data. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.