Tuesday, July 28, 2009

Can anyone solve a computer program(cpp language) ::::its the tower of hanoi.?

u can solve this only if u know the concept of tower of hanoi program.

Can anyone solve a computer program(cpp language) ::::its the tower of hanoi.?
well here is the code in "C".....I use GCC compiler.....so better u run this on that....I am not sure abt tht other compilers.....





**************************************...





#include%26lt;stdio.h%26gt;





void transfer(int n,char from[],char to[],char temp[]);


main()


{


int n;


char L[]="Left";


char C[]="Center";


char R[]="Right";


printf("Welcome to TOWERS OF HANOI\n\n");


printf("How many discs? ");


scanf("%d",%26amp;n);


printf("\n");


transfer(n,L,R,C);


}





void transfer(int n,char from[],char to[],char temp[])


{


if(n%26gt;0)


{


transfer(n-1,from,temp,to);


printf("Move disc %d from %s to %s\n",n,from,to);


transfer(n-1,temp,to,from);


}


return;


}








**************************************...
Reply:Hi there!





I'm not actually sure with the concept but the flow of program would have decrement or increment. Imagine two triangle and of it is inverted. The flow would be, the inverted triangle = 10 - 1, it would decrement by 1. Thus the other triangle would increment by 1. Somehow the program will flow like that...





Ei, hope that would help anyways to start the program you should have first the logic or flow of the system.
Reply:Dear XYZ,





rather than giving you the complete code, I am providing a link to the algorithm for this famous mathematical model.





http://hanoitower.mkolar.org/algo.html





I hope that you will get a good idea from the algo and solve the problem easily.

printable cards

No comments:

Post a Comment