Tower Of Hanoi Program In C Using Graphics In Brochures

Languages Training(C, C++, Java, VB,.NET, ASP): A2Z Computex Academy is a division of A2Z Computex Technologies, An International Web development and Software Development Company. We are the professional class Training Institute in Rajasthan with a vision to open the branches and franchise all across India. PROCEDURE DIVISION. CALL 'move-disk' USING 4, 1, 2, 3. END PROGRAM towers-of-hanoi. IDENTIFICATION DIVISION. Move-disk RECURSIVE. DATA DIVISION. LINKAGE SECTION. 01 n PIC 9 USAGE COMP. 01 from-pole PIC 9 USAGE COMP. 01 to-pole PIC 9 USAGE COMP.

Active10 months ago

I'm facing the Towers of Hanoi problem, I read the concept and the recursive way of solving it from wikipedia, but I can not see what I'm missing in the implementation of the steps mentioned in wikipedia.

I have seen many examples here but I don't want my program to print the steps, I want the program solves the problem moving the 'discs' between 3 collections, in my code I'm using 3 Stacks to simulate the pegs.

Here is my current code:

Anon DevAnon Dev
7461 gold badge8 silver badges21 bronze badges
Tower of hanoi program in c using graphics in brochures 2017

2 Answers

In order to make a recursive method you need one or more base cases where the recursion will end and then one or more recursive calls that break the problem down closer to one of the base cases. For Towers of Hanoi the idea is that moving n discs from Peg A to Peg C is just moving n-1 from Peg A to Peg B, then moving the nth from A to C and finally moving the n-1 discs from C to B. That will eventually get you down to just moving one disc which is your base case. That can be done in a recursive method very simply like this.

juharrjuharr
26.3k3 gold badges38 silver badges78 bronze badges

When you are implementing TOH, this means you are new to DS and data types. So one must use the data type which is not present in DS like stack and queue. So the below approach is using array.

using System;using static System.Console;namespace TOH{

}

Sample Program In C++ Programming

Amardeep Kumar AgrawalAmardeep Kumar Agrawal

Not the answer you're looking for? Browse other questions tagged c#recursiontowers-of-hanoi or ask your own question.

Sample program in c language

Home > Articles > Programming > C/C++

  1. The Tower of Hanoi
< BackPage 4 of 5Next >
From the author of
C++ Without Fear: A Beginner's Guide That Makes You Feel Smart, 2nd Edition
C++ Without Fear: A Beginner's Guide That Makes You Feel Smart, 2nd Edition
The Tower of Hanoi

The Tower of Hanoi

In the aforementioned movie “Battle for the Planet of the Apes,” the apes at the beginning of the story are given a test: solve a puzzle involving three poles. The first pole has several rings on it, each ring smaller than the one below it. The other two poles are empty at the start. The challenge is to move all the rings to the third pole.

The apes have to follow two rules of movement:

  1. An ape can move the top ring—and only the top ring—to any of the other two poles and drop it on the existing stack of rings that are already there, if any. (Once that ring is moved, the ape can then move the ring below it.)
  2. An ape can never place a bigger ring on top of a smaller ring.
How to program in c

One of the apes—the one that eventually takes over the world—gets to be very good at this puzzle.

Recursion in some form (either explicit or simulated) should be used to solve the puzzle for the reason I gave earlier: The correct solution involves contexts inside of contexts inside of contexts. For simple movement (move one ring from pole 1 to pole 2), the solution is trivial. The general solution is as follows.

To move N rings from a source pole to a destination pole:

  1. Move N-1 rings from the source pole to the “other” pole—that is, the pole that is neither the source nor the final destination.
  2. Move one ring from the source pole to the destination pole.
  3. Move N-1 rings from the “other” pole to the destination pole.

Once you grasp this general strategy, the C++ code is easy enough to write:

To move five rings from pole 1 to pole 3, you’d call:

This is a perfect use of recursion because it is like the “Inception” scenario I described earlier. Each level of the puzzle depends on first solving the puzzle one level deeper (or “above” it using the stack pictures shown earlier). To know where you are and what you doing at any given moment, you must remember where you are within the complex stack of cases within cases—that is, in the dream within the dream. Recursion lets the program keep track all the contexts for you.

Download getting started with spring framework pdf free download. (Eventually, you wake up and find the puzzle completely solved, just as Leonardo DiCaprio eventually wakes up and finds that everything is perfect… or does he?)

Incidentally, just as you can use recursion to simulate iteration, you can do the converse. It is always possible to simulate recursion by using a user-created stack (like the ones you can create by using the Standard Template Library). Ultimately, what matters is that you use a last-in-first-out mechanism to keep track of the current context before “popping out” to the next level.

Related Resources

  • Online Video $119.99
  • Book $31.99
  • Book $39.99