I'm a Computer Engineering student just now taking data structures. Today's lecture covered recursive functions as an easy way to solve factorial problems or sorting problems. My professor used the Towers of Hanoi example, and I really want to make sure I understand the order in which the recursive functions get called. I've tried writing out step by step how the functions execute but I'm not getting the right sequence.
I'll post my source code below, it is pretty simple. If anyone could help explain it to me, I would be really greatful. Thanks!
void hanoi(int N, char S, char D, char I){
//Step1: Anchor Value or Base Case
if(N==1){
cout <<"Move " << N << " from " << S << " to " << D << endl;
}
else {
//Step2: Make progress towards base case
hanoi(N-1, S, I, D);
cout << "Move " << N << " from " << S << " to " << D << endl;
hanoi (N-1, I, D, S);
}
}
int main(){
int N; //Number of disks
cout << "Enter number of discs: " << endl;
cin >> N;
char S = 'S'; //Source
char D = 'D'; //Destination
char I = 'I'; //Intermediary
hanoi(N, S, D, I);
system("pause");
}