I know most have you have seen this Towers of Hanoi recursive function before so hopefully someone'll be able to shed some light in how it works.
void dohanoi(int N, int from, int to, int using)
if (N > 0)
dohanoi(N-1, from, using, to); //1
printf ("move %d --> %d\n", from, to); //2
dohanoi(N-1, using, to, from); //3
dohanoi(4, 1, 3, 2);
When dohanoi() is called how does it all exactly work? I'm so lost. What happens when n=1 for example. I know that "1 --> 3" is printed but I'm exactly sure why?
How is it that anything even happens when n=1? I mean, the first line of the if statement is to call
. Shouldn't it go back to the top with the function now being
dohanoi(N-1, from, using, to);
? And then nothing happen? Why does //2 happen?
dohanoi(0, 1, 2, 3)
I would love it if someone could break down what's going on for n=1 and maybe n=2 and n=3 too.
I've looked at a factorial recursion example and it was quite simple understand, but this Tower one just isn't making sense to me. I think part of it is that it's a void function and secondly that there are multiple things under the if statement. I think I'm making more difficult than it is, but I just transferred schools and am frustrated to see that my previous school left me under prepared for this semester's courses. I'm feeling a bit overwhelmed.