Hi all I need to write a recursive function that displays every permitation of NxN numbers this example uses 3x3 and will display

0 1 0
1 1 0
2 1 0
0 2 0
1 2 0
2 2 0
0 0 1
1 0 1
2 0 1
0 1 1
1 1 1
2 1 1
0 2 1
1 2 1
2 2 1
0 0 2
1 0 2
2 0 2
0 1 2
1 1 2
2 1 2
0 2 2
1 2 2
2 2 2

using:

``````for (int c=0; c<3 ; c++)
for (int b=0; b<3 ; b++)
for(int a = 0; a<3; a++)
cout <<a<<" "<<b<<" "<<c<<endl;``````

I would like to rewrite this as a recursive function for example

displayNumbers(5,3)

which would display each combination for 5 colums of 0 to 3.

I just can't seem to figure out how to make this into a recursive function.

I would really apreciate any help as you guys seem really knolagable.

dubery.

Oh hell no! ;)

** Welcome to the forum, Dubery! **

Here's what I want you to do - start with just one number variable, and have the function call itself, until the number is 5, and print it.

You won't learn crap by having someone else do the recursive programming for you. Start simple, maybe watch or read a tutorial on the net about recursive functions, and work your way up. Then start with two variables and printing out their permutations. Finally, you will start to see the TAO of recursion, and how it can work for creating permutations.

You learn best by DOING. Post back when you have a specific question about your code (and post the code).

I can make a simple recursive function like:

``````void calc(int i,int n)
{
i--;
if (i<n)
{
cout << "i="<<i<<endl;
calc(i,n);
}
}

calc(3,3);``````

I am unsure where to procede from this point in getting multiple values to cascade and keep track of them.

@dubery

Hope this helps:

``````int a[10]; /* array to hold the numbers of the permutation */

void perm(int level)
{
int i;

if (level == 3) {
print the array
} else {
for (i = 0; i < 3; i++) {
a[level] = i;
perm(level + 1);
}
}
}``````

Many thanks, that works great and was just what I was looking for.

I can close after several house of baning my head against the wall, I was trying to do this with an array located in the function.

Thanks again, I can now de-stress for the rest of the evening.

>>I was trying to do this with an array located in the function.

That would actually be a better way instead of using a global array.

PS:
>>You learn best by DOING. Post back when you have a specific question about your code (and post the code).

As Adak said, it would be better if you try and make your method work since that is the best way to learn.

I shall be doing more work on this today, I will post back once I have finished.

I will not be using a global array as this will not fit in witht he rest of the design.

@dubery

Thanks for the link. i was looking for tutorials on Recursion as well.

@myk45 The following has more examples on recursion as well http://talkbinary.com/programming/c/recursion-examples/

Like also mentioned above, its best when you have done several examples and learned the basics on the topic you need help on. That way you can learn more to then tackle questions on your own.

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.