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 // allows program to perform input and output using namespace std;

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!