I'm looking at the code for the Tower of Hanoi. If you're not familiar with the problem, here's a wiki article on it:
http://en.wikipedia.org/wiki/Tower_of_Hanoi
You can see in the code I printed out "n == ___" The problem is I don't get why
n == 1 on the first print out. I thought it would first cout through the else conditional and it would be n == 2. Why does n == 1?
I got the code from a book, so the comments are sparse since I'm obviously a little in the dark about how it works.
Thanks in advance.
#include <iostream>
using namespace std;
void move_rings(int n, int src, int dest, int other)//recursive form of moving discs
{
if (n==1)
{cout << "1Move from " << src << " to " << dest << " n = " << n << " src = " << src << " dest = " << dest << " other = " << other << endl;
}
else
{move_rings(n-1, src, other, dest);
cout << "2Move from " << src << " to " << dest << " n = " << n << " src = " << src << " dest = " << dest << " other = " << other << endl;
move_rings(n-1, other, dest, src);
}
}
int main()
{
int n = 3;//3 discs to move among three pegs
move_rings(n,1,3,2);
return 0;
}