hi everyone. i am working on the tower of Hanoi code and got a very interesting question.
Here is the code of Tower of Hanoi problem:

#include<iostream>
 
using namespace std;
 
void move( int n, char*s, char*i, char*d )
// s stands for source tower
// d stands for destination tower
// i stands for intermediate tower
{
if( n > 0 )
{
move( n-1,s,d,i );
//cout <<"   1   "<<endl;
// move n-1 disks from source to intermediate tower
cout << "disk " << n << " is moved from " << s << " to " << d << endl;
//cout <<"   2   "<<endl;
// move the disk from to source to destination
move( n-1,i,s,d );
//cout <<"   3   "<<endl;
// move n-1 disks from intermediate to destination
}
}
 
void main()
{
cout << "\n************************************************ **********\n";
cout << "This C++ program is to solve the towers of hanoi problem";
cout << "\n************************************************ **********\n";
cout << "Enter the no. of disks ";
int n;
cin >> n;
move( n, "source tower", "intermediate tower", "destination tower" );
}

in order to understand how 2 recursive function works at the same time. I added something up to the code:

#include<iostream>
 
using namespace std;
 
void move( int n, char *s, char *i, char *d )
// s stands for source tower
// d stands for destination tower
// i stands for intermediate tower
{
	cout <<"   0    s = "<< s <<" i = "<< i << " d= "<< d <<" disk ="<< n <<endl;

if( n > 0 )
{
cout <<"   1    s = "<< s <<" i = "<< i << " d= "<< d <<" disk ="<< n <<endl;
move( n-1,s,d,i );
cout <<"	2    s = "<< s <<" i = "<< i << " d= "<< d <<" disk ="<< n <<endl;
// move n-1 disks from source to intermediate tower
cout << "DISK " << n << " IS MOVED FROM " << s << " TO " << d << endl;
cout <<"   3   s = " << s << " i = "<< i << " d= "<< d <<" disk ="<< n <<endl;
// move the disk from to source to destination
move( n-1,i,s,d );
cout <<"	4    s = "<< s <<" i = "<< i << " d= "<< d <<" disk ="<< n <<endl;
// move n-1 disks from intermediate to destination
}
}
 
void main()
{
cout << "\n************************************************ **********\n";
cout << "This C++ program is to solve the towers of hanoi problem";
cout << "\n************************************************ **********\n";
cout << "Enter the no. of disks ";
int n;
cin >> n;
move( n, "source tower", "intermediate tower", "destination tower" );
}

it looks very hard to read, but if you can understand my idea, you will be able to read that.
If you dont like that, just run the original code.
the problem is when I typed the disk = 3 and the third step of this process is
[img]http://img51.imageshack.us/img51/7521/73063794.png[/img]
By htq at 2011-02-22
disk 1 will be moved back from the destination tower to intermediate tower.
But in the code, we just have 3 directions :

move( n-1,s,d,i );
//cout <<"   1   "<<endl;
// move n-1 disks from source to intermediate tower
cout << "disk " << n << " is moved from " << s << " to " << d << endl;
//cout <<"   2   "<<endl;
// move the disk from to source to destination
move( n-1,i,s,d );
//cout <<"   3   "<<endl;
// move n-1 disks from intermediate to destination

I have not see any direction that the disk would be moved from the destination tower to intermediate tower.
Have u guys ever mentioned that or did I miss something ?
i discovered that after the line move( n-1,i,s,d ); was applied (with disk = 1 currently) , it returned to the top of the function with disk = 1 and in here, it changed it value :
s= destination tower i= source tower d= intermediate tower
IF the function move( n-1,i,s,d ) is applied , it has to be :
s = intermediate i = source d= destination
but it did in the opposite direction.
I tried with disk = 2 and it worked exactly like the the marked code
Do you guys have any idea for this ?

Do not use

void main()

use instead

int main()
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.