Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Select Healthcare Classification Systems And Databases

Authors: Katherine S. Rowell, Ann Cutrell

1st Edition

0615909760, 978-0615909769

More Books

Students also viewed these Databases questions

Question

3. The group answers the questions.

Answered: 1 week ago