Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Recursive Solution in C: // calling code: towers (4, \'A\', \'B\', \'C\'); Which

ID: 3699126 • Letter: R

Question

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

Explanation / Answer

function A is correct. If youwant to understand better please try to run the code for lower disk i.e, 2, 3.

here is the complete code..

===============================================================

Program:

===============================================================

#include <stdio.h>
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);

}


int main()
{
   //code
   towers (4, 'A', 'B', 'C');
   return 0;
}

================================================================

Sample Output:

move disk 1 from A to B
move disk 2 from A to C
move disk 1 from B to C
move disk 3 from A to B
move disk 1 from C to A
move disk 2 from C to B
move disk 1 from A to B
move disk 4 from A to C
move disk 1 from B to C
move disk 2 from B to A
move disk 1 from C to A
move disk 3 from B to C
move disk 1 from A to B
move disk 2 from A to C
move disk 1 from B to C


================================================================

KIndly Check and Verify Thanks..!!!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote