• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Towers of Hanoi....C Style :)

geno

Lifer
Ok, here goes. Last time you guys helped me out big time with a programming issue I was having. We just started learning about recursion and pointers, and it was kinda thrown at us rather quickly. It seems like this is a common exercise when it comes to learning a programming language though. The problem is, most of the students are clueless 😱 Has anyone ever done this problem out before? The object is to write a function that will move *4* disks/rings across 3 pegs, and report the moves made (if you know how the Towers of Hanoi works, you know the rules...1 disk/move at a time/bigger disks have to be on the bottom). I'm not asking for someone to do my homework for me, just a small push in the right direction since I can't get any help from my classmates (too bad we're all clueless about this assignment 🙁 ). If anyone has done this, any help is GREATLY appreciated 🙂
 
#include <iostream.h>
#include <stdlib.h>
void move ( int disk , int from , int to , int temp ) ;
void show ( int from , int to ) ;

int main()

{
int number ;
cout << "Enter the number of disks to be moved:" ;
cin >> number ;
move ( number , 1 , 3 , 2 ) ;// initial settings
cout << endl << endl ;
system("PAUSE");
return 0;
}
void move ( int disk , int from , int to , int temp )


{
if ( disk > 0 )


{
move ( disk -1 , from , temp , to ) ; // swap destination with temp to move
show ( from , to ) ; // which will print the steps onscreen
move ( disk -1 , temp , to , from ) ; }
}
void show ( int from , int to )


{
cout << "Move : " << from << "-->" << to << "\t" ;
}
 
Kickass, Thanks a lot man! I'm starting to get why this program works...I hate having to grasp these concepts but it's good to finally get it in the end 🙂 I appreciate it! Thanks 🙂
 
It's so funny how every programming book uses this. I think I have Towers of Hanoi in java somewhere around here.
 
Yeah, this one really had me confused, but I got it working finally 🙂 Now it's on to the next problem, which has me tearing my hair out (again 🙁 ) wish me luck guys...I'm barely grasping this stuff....


anyways, thanks again! 🙂
 
Back
Top