123 Eng

### Engineering the engineers™

Home
Source Codes
Engineering Colleges

Training  Reports
Seminar Reports
Placement Papers

Forums

Computer Science / IT
Electronics
Electrical
Mechanical
Chemical
Civil

CAT / MBA

GMAT / Foreign MBA
Latest Jobs

Displaying  Source Code(s)

 TOWER OF HANOI -------------------------------------------------------------------------------- Description : solution of tower of hanoi /*for any doubt any query mail me back ravindra@iitg.ernet.in*/ /*---------------sumitted by Ravindra kumar-------------------*/ /*-----------tested in Unix/Linux plateform-------------------*/ #include //the c++ standard library for stream input output #include //the c standard library for standard input output #include //for the exit function using namespace std; class arr //arr class that holds each stag { public: int a[100],b[100],c[100]; int topa,topb,topc; }hanoi; int move=1; //counts the no. of moves int main() { void tower(int ,int *,int *,int *,int *,int *,int *); //function prototype void show(int); //function prototype int i; system("clear"); cout<<"Enter the no of elements for which u want to solve the problem
; scanf("%d",&i); system("clear"); for(int j=0;j; for(int j=i-1;j>-1;j--) //show the statusm of each arr { show(hanoi.a[j]); show(hanoi.b[j]); show(hanoi.c[j]); cout<<"
; } cout<<"
; tower(i,hanoi.a,hanoi.c,hanoi.b,&(hanoi.topa),&(hanoi.topc),&(hanoi.topb)) ; //call to do the job } void tower(int n,int src[],int dest[],int aux[],int *ts,int *td,int *ta) //the tower function passes { //the arrs along with the top pointers void show (int); if(n==1) //if one element is there in source arr , { //then it is moved to the destination arr, dest[++(*td)]=src[(*ts)]; src[*ts]=-1; (*ts)--; int max; max=((*ts)>(*td)?(*ts):(*td)); max=(max>(*ta)?max:(*ta)); cout<<" Move "<; for(int i=max;i>-1;i--) //status of arrs shown { show(hanoi.a[i]); show(hanoi.b[i]); show(hanoi.c[i]); cout<<"
; } cout<<"
; return; } tower(n-1,src,aux,dest,ts,ta,td); //else the tower(1,src,dest,aux,ts,td,ta); // problem is solved by tower(n-1,aux,dest,src,ta,td,ts); // recursive calls } void show(int a) //the show function shows the current status of the arrs { if(a==-1) cout<<"- "; else cout<

Contribute content or training reports / feedback / Comments
job placement papers