Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Towers of Hanoi

Posted on 2006-05-18
Medium Priority
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 85

Accepted Solution

ozo earned 2000 total points
ID: 16715073
 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

580 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