Question 1: Complete the TODO part of “towersGeneral”
function.
Description: Move the disk stack from the first peg to the last peg
for N number of disks and pegs. Following diagram just shows the
example when user enters N=3.
Challenge: How would you generalize your solution to N disks
instead of just 3?
INSTRUCTIONS: • In case of cheated/copied/plagiarized code, you will get zero. • Late submission are not accepted. • Only submissions over UBIS system will be accepted. Question 1: Complete the TODO part of “towers General” function. Description: Move the disk stack from the first peg to the last peg for N number of disks and pegs. Following diagram just shows the example when user enters N=3. • Goal: Move the disk stack from the first peg to the last peg Beginning Solution 2 Disks Source Auxiliary Destination Auxiliary Destination Source • Rules: Can only move one disk at a time 0 You cannot place a larger disk on top of a smaller disk 3 DISKS (1) A B C A B C (2) (3) A B C A B C A B C 5) (6) "11+11+"| A в с A B с A в с Challenge: How would you generalize your solution to N disks instead of just 3?
Code: #include "stack.h" using namespace std; // How would you generalize your solution to N disks instead of just 3? void towersGeneral(Stack<int>& destination, int numDisks) { // populate source stack with numDisks initial disks Stack<int> source; for (int i = numDisks; i > 0; i--){ source.push(1); } Stack<int> auxiliary; /* TODO: Challenge problem */ } int main(){ Stack<int> result; \\ The final peg // Challenge problem: write a general towers General function // result = {}; // towers General (result, 5); cout<<result<<endl; // Show the final peg return 0; }
Question 1: Complete the TODO part of “towersGeneral” function. Description: Move the disk stack from the first peg to t
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am