Program to Implement Tower of Hanoi 1. Create function hanoi that takes the number of disks n and the names of the source, auxiliary and target pegs as arguments. 2. The base case is when the number of disks is 1, in which case simply move the one disk from source to target and return. 3. Move n - 1 disks from source peg to auxiliary peg using the target peg as the auxiliary. 4. Move the one remaining disk on the source to the target. 5. Move the n-1 disks on the auxiliary peg to the target peg using the source peg as the auxiliary.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter12: Points, Classes, Virtual Functions And Abstract Classes
Section: Chapter Questions
Problem 19SA
icon
Related questions
Question

urgently needed

Program to Implement Tower of Hanoi
1. Create function hanoi that takes the number of disks n and the names of the source, auxiliary and target pegs as arguments.
2. The base case is when the number of disks is 1, in which case simply move the one disk from source to target and return.
3. Move n- 1 disks from source peg to auxiliary peg using the target peg as the auxiliary.
4. Move the one remaining disk on the source to the target.
5. Move the n - 1 disks on the auxiliary peg to the target peg using the source peg as the auxiliary.
Case 1:
Enter number of disks: 4
Move disk 1 from peg A to peg B.
Move disk 2 from peg A to peg C.
Move disk 1 from peg B to peg C.
Move disk 3 from peg A to peg B.
Move disk 1 from peg C to peg A.
Move disk 2 from peg C to peg B.
Move disk 1 from peg A to peg B.
Move disk 4 from peg A to peg C.
Move disk 1 from peg B to peg C.
Move disk 2 from peg B to peg A.
Move disk 1 from peg C to peg A.
Move disk 3 from peg B to peg C.
Move disk 1 from peg A to peg B.
Move disk 2 from peg A to peg C.
Move disk 1 from peg B to peg C.
Transcribed Image Text:Program to Implement Tower of Hanoi 1. Create function hanoi that takes the number of disks n and the names of the source, auxiliary and target pegs as arguments. 2. The base case is when the number of disks is 1, in which case simply move the one disk from source to target and return. 3. Move n- 1 disks from source peg to auxiliary peg using the target peg as the auxiliary. 4. Move the one remaining disk on the source to the target. 5. Move the n - 1 disks on the auxiliary peg to the target peg using the source peg as the auxiliary. Case 1: Enter number of disks: 4 Move disk 1 from peg A to peg B. Move disk 2 from peg A to peg C. Move disk 1 from peg B to peg C. Move disk 3 from peg A to peg B. Move disk 1 from peg C to peg A. Move disk 2 from peg C to peg B. Move disk 1 from peg A to peg B. Move disk 4 from peg A to peg C. Move disk 1 from peg B to peg C. Move disk 2 from peg B to peg A. Move disk 1 from peg C to peg A. Move disk 3 from peg B to peg C. Move disk 1 from peg A to peg B. Move disk 2 from peg A to peg C. Move disk 1 from peg B to peg C.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
ADT and Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,