Question: Recursive Solution in C: // calling code: towers (4, 'A', 'B', 'C'); Which of the two alternatives below works? Trace them! // function alt 1:

 Recursive Solution in C: // calling code: towers (4, 'A', 'B',

Recursive Solution in C:

// calling code:

towers (4, 'A', 'B', 'C');

Which of the two alternatives below works? Trace them!

// function alt 1:

void towers(int n, char cFromPeg, char cAuxPeg, char cToPeg)

{

// only one disk to move

if (n == 1)

{

printf("move disk %d from %c to %c ", n, cFromPeg, cToPeg);

return;

}

// move the top n-1 disks to the auxPeg using the toPeg as an aux

towers(n-1, cFromPeg, cToPeg, cAuxPeg);

// move disk n

printf("move disk %d from %c to %c ", n, cFromPeg, cToPeg);

// move the top n-1 disks from auxPeg to toPeg using fromPeg as an aux

towers(n-1, cAuxPeg, cFromPeg, cToPeg);

}

// function alt 2:

void towers(int n, char cFromPeg, char cAuxPeg, char cToPeg)

{

// only one disk to move

if (n == 1)

{

printf("move disk %d from %c to %c ", n, cFromPeg, cToPeg);

return;

}

// move the top n-1 disks to the auxPeg using the toPeg as an aux

towers(n-1, cFromPeg, cToPeg, cAuxPeg);

// move disk n

printf("move disk %d from %c to %c ", n, cFromPeg, cToPeg);

// move the top n-1 disks from auxPeg to fromPeg using toPeg as an aux

towers(n-1, cAuxPeg, cToPeg, cFromPeg);

}

Which function to solved this?

Towers of Hanoi Week 11 Move the disks from peg A to peg C, one disk at a time without a larger disk being on top of a smaller disk. Assume disk 4 is the green bottom disk, disk 3 is the red disk, disk 2 is the yellow disk, and disk 1 is the blue disk. step 2 Goal is to have disk 4 on the bottom of peg C. That means we need disk 3 on the bottom of peg B; therefore, disk 2 needs to be on the bottom of peg C. So, we start by moving disk 1 to peg B. Step 1

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!