Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

PostgreSQL Up And Running A Practical Guide To The Advanced Open Source Database

Authors: Regina Obe, Leo Hsu

3rd Edition

1491963417, 978-1491963418

More Books

Students also viewed these Databases questions

Question

In what ways are job-order and process costing similar?

Answered: 1 week ago

Question

1. Outline the listening process and styles of listening

Answered: 1 week ago

Question

4. Explain key barriers to competent intercultural communication

Answered: 1 week ago