Question 1: Complete the TODO part of “towersGeneral” function. Description: Move the disk stack from the first peg to t

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Question 1: Complete the TODO part of “towersGeneral” function. Description: Move the disk stack from the first peg to t

Post by answerhappygod »

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?
Question 1 Complete The Todo Part Of Towersgeneral Function Description Move The Disk Stack From The First Peg To T 1
Question 1 Complete The Todo Part Of Towersgeneral Function Description Move The Disk Stack From The First Peg To T 1 (147.72 KiB) Viewed 42 times
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; }
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply