Question
//Towers of Hanoi #include // allows program to perform input and output using namespace std; void TowersOfHanoi(int, char, char, char); int main() { int n;
//Towers of Hanoi
#include
void TowersOfHanoi(int, char, char, char);
int main() { int n;
cout << "A Program to perform Towers of Hanoi using recursion."; cout << "Enter the number of discs: "; cin >> n;
TowersOfHanoi(n, '1', '2', '3');
return 0; } void TowersOfHanoi(int n, char peg1, char peg2, char peg3) { if (n != 0) { TowersOfHanoi(n - 1, peg1, peg3, peg2);
cout << "Move Disc " << n << " :" << peg1 << "->" << peg3 << endl; TowersOfHanoi(n - 1, peg2, peg1, peg3);
} }
Hi,
This is the code that I wrote at C++. But, my output should be like this:
Total moves needed: Disc 1: Peg 1 -> Peg 3 Disc 2: Peg 1 -> Peg 2 Disc 1: Peg 3 -> Peg 2 Disc 3: Peg 1 -> Peg 3 Disc 1: Peg 2 -> Peg 1 Disc 2: Peg 2 -> Peg 3 Disc 1: Peg 1 -> Peg 3 7 moves
Could you please help me to correct my code. I need exact output with total moves. Thanks for your help.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started