Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have this code for a Tower of Hanoi and I have to debug it, I'm stuck and not sure where to go. I could

I have this code for a Tower of Hanoi and I have to debug it, I'm stuck and not sure where to go. I could use some help finding what the errors are in this code.

int main() { const int STARTING_PEG = 0; // 0 - ID of the peg our disks start on const int TARGET_PEG = 2; // 2 - ID of the peg we need to move our disks to const int SPARE_PEG; // 1 - ID of the peg that can be used for temp storage int numberOfDisks; cout << "Enter number of disks to solve: "; cin >> numberOfDisks; // user enters the number of disks to solve for // solve Towers of Hanoi problem for numberOfDisks disks // disks are initially on our starting peg and need to move to our target peg towersOfHanoi( numberOfDisks, STARTING_PEG, SPARE_PEG, TARGET_PEG ); return 0; } // // towersOfHanoi() // // Recursive solution to move N disks from a starting peg to a // destination peg // // Params: // int N - the number of disks to move // int start - ID of the peg the disks are currently on // int target - ID of the peg the disks need to move to // int spare - ID of the peg that can be used as temporary storage // // Return: // void - returns nothing // // Output: // Outputs the series of moves to solve the problem for N disks // void towersOfHanoi( int N, int start, int target, int spare ) { // if there are no more disks to move, then we are done! if( N == 1 ) return; // Step 1: recursively move the top N-1 disks from our current peg // to the non-target peg towersOfHanoi( N-1, start, spare, target ); // Step 2: move the largest disk that was on the bottom from our // current peg to the target peg cout << "Move disk " << N << " from " << start << " to " << target << endl; // Step 3: recursively move the remaining N-1 disks from the non-target // peg to our target peg on top of the largest disk we just moved towersOfHanoi( spare, target, start ); } 

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

Larry Ellison Database Genius Of Oracle

Authors: Craig Peters

1st Edition

0766019748, 978-0766019744

More Books

Students also viewed these Databases questions