Question: help with command line and dynamic allocation my code: #include using namespace std; void print_array(int b[][3]){ int i,j; cout for(i=0;i for(j=0;j cout } cout }

help with command line and dynamic allocation

help with command line and dynamic allocation my code: #include using namespace

my code:

#include

using namespace std;

void print_array(int b[][3]){

int i,j;

cout

for(i=0;i

for(j=0;j

cout

}

cout

}

}

void move_disk(int disks, int b[][3], int from_col, int to_col){

int i, min;

for(i = 2; i>=0 && b[i][from_col]!=0; i--){

disks = i;

}

for(i = 2; i>=0; i--){

if (b[i][to_col]==0){

min = i;

break;

}

}

b[min][to_col] = b[disks][from_col];

b[disks][from_col] = 0;

}

void towers(int disks, int b[][3], int from_col, int to_col, int spare){

if(disks>=1){

towers(disks-1, b, from_col, spare, to_col);

move_disk(disks-1, b, from_col, to_col);

print_array(b);

towers(disks-1, b, spare, to_col, from_col);

}

}

int main() {

int b[3][3]={{1,0,0},{2,0,0},{3,0,0}};

print_array(b);

towers(3, b, 0, 1, 2);

return 0;

}

Begin by designing these two functions, towers() and print array). To help you out, your towers() function will be recursive with the following prototype: void towers(Int disks, int b[3], Int from_col, int to,col, int spare); Here is an outline of the recursive towers function: If(number of disks is >= 1) Call Towers with (disks-1, b, from_col, spare, to col) Move the disk Print the board Call Towers with (disks-1, b, spare, to col, from col) Dynamically Allocated 2-D array (5 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. ntinue 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 th out, your towers() function will be change to the following prototype: an statically allocated. To help you

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!