Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Need code in C++ Worksheet: Design First, begin by writing the steps on a piece of paper that represents the moves among the columns. For
Need code in C++
Worksheet: Design First, begin by writing the steps on a piece of paper that represents the moves among the columns. For instance, with three disks, the smallest disk from the 1st post will be moved to the second peg, i.e. 1 -> 2. Then, the 2nd disk will be moved to the 3rd peg, i.e. 1->3, etc Write the steps for the base case, n 1 disks, n 2 disks, and n 3 disks. You should notice that you have 2n 1 moves for each of these cases. Also, note any pattern that you see, i.e. when do you see the base case. Here is an outline of the recursive towers function: void towers(int number_of_disks, int b[1[3], int from_col, int to_col, int spare) If(number of disks is1) Call Towers with (number_of_disks-1, b, from_col, spare, to_col) Move the disk Print the board Call Towers with (number_of_disks-1, b, spare, to_ col, from_ col) As a group with the TAs, walk through the algorithm provided for the towers() function with a board that has 1 disk and 3 columns, then 2 disks and 3 columns, and 3 disks with 3 columns e.g.. towers(1, 1, 2, 3);, towers(2, 1, 2, 3);, towers(3, 1, 2, 3), etc Provide the example walk through for the following calls: towers(1, 1, 2, 3); towers(2, 1, 2, 3); towers (3, 1, 2, 3); Implementation Statically Allocated 2-D array (2 pts) First, you can implement this is using a static 2-D array with 3 columns for the 3 posts and 3 rows, and you can initialize the array with the numbers 1, 2, and 3 in the first column to represent the initial state of the game. The goal is to print out the board after each move in the game, seeing the following output. Example with two disks 1 0 0 2 0 0 2 0 1 0 2 1 0 1 0 0 2 0 Dynamically Allocated 2-D array (3 pts) Next, implement this is using a dynamically allocated 2-D array with 3 columns for the 3 posts and N rows for N disks. Get the number of disks from the user as a command-line argument, i.e. towers 5 Continue to initialize the array with the numbers corresponding to the disks in the first column and Os in all other columns to represent the initial state of the game. You should now see the above example output, given 2 for the number of disks Remember to change your towers) and print_array) function parameters to accept dynamically allocated arrays, rather than statically allocated. To help you out, your towers() function will be change to the following prototype void towers(int number_of_disks, int *b, int from_col, int to_col, int spare); Make sure you delete your board after calling the towers function Create/Delete Functions for Dynamically Allocated 2-D array If you haven't done so already, create functions for creating and deleting the array on the heap Make sure you set the board back to null in the delete function! Run your program through valgrind to make sure you do not have any memory leaks!!! Worksheet: Design First, begin by writing the steps on a piece of paper that represents the moves among the columns. For instance, with three disks, the smallest disk from the 1st post will be moved to the second peg, i.e. 1 -> 2. Then, the 2nd disk will be moved to the 3rd peg, i.e. 1->3, etc Write the steps for the base case, n 1 disks, n 2 disks, and n 3 disks. You should notice that you have 2n 1 moves for each of these cases. Also, note any pattern that you see, i.e. when do you see the base case. Here is an outline of the recursive towers function: void towers(int number_of_disks, int b[1[3], int from_col, int to_col, int spare) If(number of disks is1) Call Towers with (number_of_disks-1, b, from_col, spare, to_col) Move the disk Print the board Call Towers with (number_of_disks-1, b, spare, to_ col, from_ col) As a group with the TAs, walk through the algorithm provided for the towers() function with a board that has 1 disk and 3 columns, then 2 disks and 3 columns, and 3 disks with 3 columns e.g.. towers(1, 1, 2, 3);, towers(2, 1, 2, 3);, towers(3, 1, 2, 3), etc Provide the example walk through for the following calls: towers(1, 1, 2, 3); towers(2, 1, 2, 3); towers (3, 1, 2, 3); Implementation Statically Allocated 2-D array (2 pts) First, you can implement this is using a static 2-D array with 3 columns for the 3 posts and 3 rows, and you can initialize the array with the numbers 1, 2, and 3 in the first column to represent the initial state of the game. The goal is to print out the board after each move in the game, seeing the following output. Example with two disks 1 0 0 2 0 0 2 0 1 0 2 1 0 1 0 0 2 0 Dynamically Allocated 2-D array (3 pts) Next, implement this is using a dynamically allocated 2-D array with 3 columns for the 3 posts and N rows for N disks. Get the number of disks from the user as a command-line argument, i.e. towers 5 Continue to initialize the array with the numbers corresponding to the disks in the first column and Os in all other columns to represent the initial state of the game. You should now see the above example output, given 2 for the number of disks Remember to change your towers) and print_array) function parameters to accept dynamically allocated arrays, rather than statically allocated. To help you out, your towers() function will be change to the following prototype void towers(int number_of_disks, int *b, int from_col, int to_col, int spare); Make sure you delete your board after calling the towers function Create/Delete Functions for Dynamically Allocated 2-D array If you haven't done so already, create functions for creating and deleting the array on the heap Make sure you set the board back to null in the delete function! Run your program through valgrind to make sure you do not have any memory leaksStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started