School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,363 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,457 people online right now. Registration is fast and FREE... Join Now!




Towers of Hanoi

 

Towers of Hanoi

AfterBurner66

12 Sep, 2008 - 02:25 PM
Post #1

New D.I.C Head
*

Joined: 2 Aug, 2008
Posts: 31



Thanked: 2 times
My Contributions
Hi all!
As a person who has not a Computer Science degree(I am a poor web developer!),but having much desire to learn as much as I can about CS topics,I have a (maybe easy huh.gif) question about the recursive algorithm that solves the "Towers of Hanoi" puzzle.
I can understand that generally,having n discs and A,B,C pegs,the strategy is:
1.move (n-1) discs from A to B.
2.move nth disc from A to C.
3.move the (n-1) discs from B to C.Done!
but this is implemented in whatever procedural language with a function,and the one thing that changes,is the order of arguments.OK so far but how do we assure that algorithm does recursively the moves that must be done and not something else?In other words how to be sure that changing of passed arguments leads to the correct solution?Because of two recursive functions used(I have studied implementations in Pascal,C,Java so far),it was difficult for me to construct a tracetable,because at the point of second recursive function call,things get much complicated I think,due to the fact that on return of function call,first recursive function is encountered first and after that we go to second etc.
So is there any idea about how to construct a tracetable "safely",or at least how to trace the whole procedure?
I tried also to solve the problem with the physical moves of discs required,but I didn't manage to map this to the recursive function calls.And something maybe more elementary:in what sense do we have recursion in this problem/
Any help or resource would be appreciated.Thanks in advance!


User is offlineProfile CardPM
+Quote Post


AdamSpeight2008

RE: Towers Of Hanoi

12 Sep, 2008 - 05:42 PM
Post #2

The Bandido Coder
Group Icon

Joined: 29 May, 2008
Posts: 2,685



Thanked: 155 times
Dream Kudos: 3925
Expert In: vb.net, LINQ

My Contributions
I hope this helps. It is a vb.net console app version.
When run it will visually show you the current depth of recursion and the variables values.
vb

Module Module1
Const ShowTrace As Boolean = True
Dim level As Integer = 0
Dim i As Integer = 0
Dim c As Char = " "

Sub Main()
Hanoi(6)
Console.ReadKey()
End Sub

Private Sub Hanoi(ByVal n As Integer)
i = 0
Call DoHanoi(1, 2, 3, n)
End Sub


Private Sub DoHanoi(ByVal f As Integer, ByVal u As Integer, ByVal t As Integer, ByVal n As Integer)
If ShowTrace Then level += 1
If ShowTrace Then Console.WriteLine(Strings.StrDup(level, c) & "Entering DoHanoi({0},{1},{2},{3})", f, u, t, n)
If n = 1 Then
i += 1
Console.WriteLine(IIf(ShowTrace, Strings.StrDup(level, c), "") & "{0}. Move {1} to {2} ", i, f, t)
Else
DoHanoi(f, t, u, n - 1)
DoHanoi(f, u, t, 1)
DoHanoi(u, f, t, n - 1)
End If
If ShowTrace Then Console.WriteLine(Strings.StrDup(level, c) & "Exiting DoHanoi")
If ShowTrace Then level -= 1

End Sub
End Module

User is offlineProfile CardPM
+Quote Post

AfterBurner66

RE: Towers Of Hanoi

13 Sep, 2008 - 06:41 PM
Post #3

New D.I.C Head
*

Joined: 2 Aug, 2008
Posts: 31



Thanked: 2 times
My Contributions
Thanks a lot AdamSpeight2008!This really helps.Though I don't know much VB,this is the better "towers of hanoi" program I've ever seen, by far.I can trace the procedure very well.Thanks again!
User is offlineProfile CardPM
+Quote Post

crazydia143

RE: Towers Of Hanoi

30 Mar, 2009 - 07:35 AM
Post #4

New D.I.C Head
*

Joined: 30 Mar, 2009
Posts: 2

can ne1 pls give me src code 4 TOWERS OF HANOI using openGL.
in DEV C++
User is offlineProfile CardPM
+Quote Post

Gloin

RE: Towers Of Hanoi

30 Mar, 2009 - 08:37 AM
Post #5

Expert Schmexpert...
Group Icon

Joined: 4 Aug, 2008
Posts: 3,528



Thanked: 143 times
Dream Kudos: 75
My Contributions
*Sigh*
User is offlineProfile CardPM
+Quote Post

firebolt

RE: Towers Of Hanoi

3 Apr, 2009 - 04:19 AM
Post #6

D.I.C Lover
Group Icon

Joined: 20 Feb, 2009
Posts: 5,463



Thanked: 75 times
Dream Kudos: 1675
My Contributions
haha... why not i just say:

anyone have the code for VB6
User is online!Profile CardPM
+Quote Post

griffinfujioka

RE: Towers Of Hanoi

30 Apr, 2009 - 05:03 PM
Post #7

New D.I.C Head
*

Joined: 26 Oct, 2008
Posts: 11

recursion smile.gif
User is offlineProfile CardPM
+Quote Post

mattman059

RE: Towers Of Hanoi

30 Apr, 2009 - 05:55 PM
Post #8

D.I.C Addict
Group Icon

Joined: 23 Oct, 2006
Posts: 537



Thanked: 8 times
Dream Kudos: 225
My Contributions
QUOTE(crazydia143 @ 30 Mar, 2009 - 09:35 AM) *

can ne1 pls give me src code 4 TOWERS OF HANOI using openGL.
in DEV C++




*Sigh*..It's times like this when i want to say "Sure!"

CODE

cout << "The answer is: "<< endl;
cout << "IF (WINDOWS) THEN : Press Windows Key, U, U...Then step away" << endl;
cout << "IF (*NIX) THEN: Open Console, type sudo rm -r, your password ...Then step away" << endl;

User is offlineProfile CardPM
+Quote Post

jcmaster2

RE: Towers Of Hanoi

1 May, 2009 - 07:47 PM
Post #9

D.I.C Head
**

Joined: 27 Apr, 2009
Posts: 139



Thanked: 4 times
My Contributions
Towers of Hanoi is typically found in almost every programming book in most languages with recursion....look it up...also the web...
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 08:23PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month