Question: In C++ ONLY Begin by reviewing the recursive solution to the task Towers of Hanoi . Once you feel comfortable with the process, now return

In C++ ONLY

Begin by reviewing the recursive solution to the task Towers of Hanoi . Once you feel comfortable with the process, now return to the AutoGrader. A partial solution has been started for you. Your job is to correct this implementation so that the function is called recursively properly and solves the Towers of Hanoi task. Reminder that clicking on the red bars will tell you the syntax error or expected output.

#include // needed for cout, endl, & cin

using namespace std; // so we don't have to type std::cout, std::cin, std::endl everytime

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

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!