Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8578131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:19:01+00:00 2026-06-11T20:19:01+00:00

I was implementing the tower of hanoi problem to understand more about recursion. I

  • 0

I was implementing the tower of hanoi problem to understand more about recursion. I was able to implement it using the 3 peg case, however, when I wanted to use more pegs (to generate less moves) I understand the Frame-Stewart solution where I have to break the number of disks I have and stack onto one peg and never touch that peg while I am transferring disks from the source peg to the destination pegs with all the intermediate pegs. However, I don’t understand how write something like move(disks, 1, i, {2…K}) as a function. How can I write the names of all the pegs in the function prototype when I don’t even know them from the beginning? I have given below what I worked up for the 3 disk solution, but I need help with the more general case.

// private member function: primitive/basic move of the top disk
    // from the source peg to the destination peg -- and legality-check
    void move_top_disk(int source, int dest)
    {
        cout << "Move disk " << towers[source].front() << " from Peg ";
        cout << source+1 << " to Peg " << dest+1;
        towers[dest].push_front(towers[source].front());
        towers[source].pop_front();
        if (true == is_everything_legal())
            cout << " (Legal)" << endl;
        else 
            cout << " (Illegal)" << endl;
    }

    // private member function: recursive solution to the 3 Peg Tower of Hanoi
    void move(int number_of_disks, int source, int dest, int intermediate)
    {
        if (1 == number_of_disks)
        {
            moves++;
            move_top_disk (source, dest);
        }
        else 
        {
            moves++;
            move (number_of_disks-1, source, intermediate, dest);
            move_top_disk (source, dest);
            move (number_of_disks-1, intermediate, dest, source);
        }
    }

I don’t understand what kind of changes I would need to make to my “move” function to solve for a 6 pegs case. Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:19:02+00:00Added an answer on June 11, 2026 at 8:19 pm

    You will need to modify the last block of code, where it does move, move-top-disk, move.

    And here is the article regarding solution of Hanoi towers for N pegs: Towers of Hanoi with K pegs

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When implementing a Matrix construct using arrays, which would be more efficient? Using a
i'm trying to make tower defense game, i'm implementing game map for fast lookup
implementing publishActivity in PHP using the REST API using this code: $activity = array(
Implementing a simple Login screen using JSF and Spring and Hibernate. I have written
When implementing a hash table using a good hash function (one where the probability
Before implementing j_security_check using MySQL realm authentication in my web app. I had the
Implementing Document-View pattern I faced the problem: standard generic collections and dictionaries ( List<T>
When implementing a Facebook Canvas app using an iframe the iframe does a post
Implementing the basic algorithm using last array as a pivot in Java, is it
When implementing iterator using yield return , is there any difference between returning IEnumerator

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.