Question

Part A [50] in c++ A toy that many children play with is a base with...

Part A [50]

in c++
A toy that many children play with is a base with three pegs and five disks of different diameters. The disks begin on one peg, with the largest disk on the bottom and the other four disks added on in order of size. The idea is to move the disks from the peg they are on to another peg by moving only one disk at a time and without ever putting a larger disk on top of a smaller one. This child's toy is actually an example of a classic mathematical puzzle called the Towers of Hanoi problem.
Write a recursive solution to this problem. It may take you a while to see the solution, but the program itself is quite short.
Consider the source peg is A, the temporary peg is B, and the destination peg is C.
Sample Input/output
Enter number of disks: 1
Solution: A→C
Enter number of disks: 2
Solution: A→B A→C B→C
Enter number of disks: 3
Solution:A→C A→B C→B A→C B→A B→C A→C

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries


#include <iostream>

#include <string>


using namespace std;


void hanoi(int n, string source, string destination, string help) {

if (n == 1) {


cout<<source<< "->" << destination << " ";

} else {

hanoi(n - 1, source, help, destination);

cout << source << "->" << destination << " ";

hanoi(n - 1, help, destination, source);

}

}


int main() {

int disks;

cout << "Enter number of disks:";

cin >> disks;

hanoi(disks, "A", "C", "B");

return 0;

}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Part A [50] in c++ A toy that many children play with is a base with...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT