Towers of Hanoi

Posted on 2006-05-18
Last Modified: 2010-04-02

Need help converting from recursion to iteration, no clue.  Please advise.  Del

/* iteration example:

#include <cstdio>
#include <ctime>

#define N   29
void hanoi(void) {

     int x, fr, to;
     unsigned int i, j;

     for(x = 1; x < (1 << N); x++) {

          i = (x|x-1) + 1; to = (i+i/3) &3;
          i = x& x-1; fr = (i+i/3) &3;
          for(i = x, j = 1; ; i >>= 1, j++) { if(i&1) break; }
          #ifdef AUSGABE
               printf( "move disk %i from %i to %i\n", j, fr, to );



int main (void) {

     clock_t t0, t1;
     printf("Time: %f s\n",(t1-t0)/(float)CLOCKS_PER_SEC);

       return 0;


// recursive

#include <iostream>
#include <iomanip>

using namespace std;

void move(unsigned n, unsigned& moveNumber, char source, char destination, char spare);

int main() {

      const char PEG1 = 'A', PEG2 = 'B', PEG3 = 'C';

      unsigned moveNumber = 0;
      cout << "This program solves the Hanoi Towers puzzle.\n\n";
      cout << "Enter the number of disks. " << endl;

      int numDisks;
      cout << endl;

      move(numDisks, moveNumber, PEG1, PEG3, PEG2);


void move(unsigned n, unsigned& moveNumber, char source, char destination, char spare) {

      if(n == 1) {
            cout << setw(3) << moveNumber << ". Move the top disk from " << source << " to " << destination << endl;

      else {
            move(n - 1, moveNumber, source, spare, destination);
            move(1, moveNumber, source, destination, spare);
            move(n - 1, moveNumber, spare, destination, source);


Question by:edelossantos
    1 Comment
    LVL 84

    Accepted Solution

     char *disk[]={"A","B","C"};
      for( int i=1; i < (1 << n); i++){
        cout << setw(3) << i << ". Move the top disk from " << disk[(i&i-1)%3] << " to " << disk[((i|i-1)+1)%3] << endl;

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
    Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now