Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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

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_2

Step: 3

blur-text-image_3

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

Excel As Your Database

Authors: Paul Cornell

1st Edition

1590597516, 978-1590597514

More Books

Students also viewed these Databases questions

Question

6. Verify or validate data obtained from other sources.

Answered: 1 week ago

Question

121. If X is uniformly distributed on [1, 3], find the pdf of Y X2.

Answered: 1 week ago