Given n (=3) discs: disc1, disc2, diskc3 stacked on the leftmost peg in order of
ID: 3777960 • Letter: G
Question
Given n (=3) discs: disc1, disc2, diskc3 stacked on the leftmost peg in order of size with the largest disc at the bottom and a disc is always smaller than the disc below. Move all three (n) discs from leftmost Peg A to rightmost Peg C with the following rules: Only one disc may be moved at a time. A disc cannot be placed on the top of a smaller disc. There are only three pegs: A, B, C Write the program to display every move and indicate the position of each disc: Beginning position of three discs: (Position of disc 1, Position of disc 2, position of disc 3) Disc 1 is smaller than disc 2 and disc 2 is smaller than disc 3. Move disc 1 from Peg A to Peg C Move disc 2 from Peg A to Peg B (C, B, A) Move disc 1 from Peg C to Peg B (B, B, A) Etc. _________ Last move Move disc 1 from Peg A to Peg C (C, C, C) Do the same for n=4 discs. Do the same for n= 5 discs. Your program output must show proper information to be understood well by the reader/viewer.Explanation / Answer
#include <iostream>
using namespace std;
void hanoiTowers(int, char, char, char);
int main()
{
int num_of_disk;// variable to store number of disk
cout<<"Enter the number of disks : ";
cin>>num_of_disk;
cout<<"The sequence of moves involved in the Tower of Hanoi are : ";
hanoiTowers(num_of_disk, 'A', 'C', 'B');//recursive call to the hanoiTowers method
return 0;
}
//recursive method to solve tower of hanoi
void hanoiTowers(int num, char frompeg, char topeg, char auxpeg)
{
//base condition to return from the recursion
if (num == 1)
{
cout<<" Move disk 1 from peg "<<frompeg<<" to peg "<<topeg;
return;
}
hanoiTowers(num - 1, frompeg, auxpeg, topeg);
cout<<" Move disk "<<num<<" from peg "<<frompeg<<" to peg "<<topeg;
hanoiTowers(num - 1, auxpeg, topeg, frompeg);
}
-------output------------
Enter the number of disks : 3 The sequence of moves involved in the Tower of Hanoi are : Move disk 1 from peg A to peg C
Move disk 2 from peg A to peg B
Move disk 1 from peg C to peg B Move disk 3 from peg A to peg C Move disk 1 from peg B to peg A Move disk 2 from peg B to peg C Move disk 1 from peg A to peg C
Enter the number of disks : 4 The sequence of moves involved in the Tower of Hanoi are : 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
Enter the number of disks : 5 The sequence of moves involved in the Tower of Hanoi are : Move disk 1 from peg A to peg C Move disk 2 from peg A to peg B Move disk 1 from peg C to peg B Move disk 3 from peg A to peg C Move disk 1 from peg B to peg A Move disk 2 from peg B to peg C Move disk 1 from peg A to peg C Move disk 4 from peg A to peg B Move disk 1 from peg C to peg B Move disk 2 from peg C to peg A Move disk 1 from peg B to peg A Move disk 3 from peg C to peg B Move disk 1 from peg A to peg C Move disk 2 from peg A to peg B Move disk 1 from peg C to peg B Move disk 5 from peg A to peg C Move disk 1 from peg B to peg A
Move disk 2 from peg B to peg C Move disk 1 from peg A to peg C Move disk 3 from peg B to peg A Move disk 1 from peg C to peg B Move disk 2 from peg C to peg A Move disk 1 from peg B to peg A Move disk 4 from peg B to peg C Move disk 1 from peg A to peg C Move disk 2 from peg A to peg B Move disk 1 from peg C to peg B Move disk 3 from peg A to peg C Move disk 1 from peg B to peg A Move disk 2 from peg B to peg C Move disk 1 from peg A to peg C
//Note: Feel free to ask any queries/doubts. God bless you!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.