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

C Programming Only. (pgs 73-75 of section 2.4 included below) //----------------

ID: 3750103 • Letter: C

Question

C Programming Only. (pgs 73-75 of section 2.4 included below)

//------------------------------------------------------------

// Problem #33

// Problem33.c

//------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

#include <time.h>

#include <assert.h>

#define USE_INSTRUMENTED_CODE

int numberOfMoves;

//------------------------------------------------------------

typedef struct

//------------------------------------------------------------

{

   int size;

   int *disks;

} PEG;

//------------------------------------------------------------

int main()

//------------------------------------------------------------

{

   void RunOneTrial(const int n,PEG pegs[]);

   int LB,UB,n;

   PEG pegs[3+1];

   printf("LB? "); scanf("%d",&LB);

   printf("UB? "); scanf("%d",&UB);

   for (n = LB; n <= UB; n++)

   {

   // Allocate peg disk space for n disks (ignore pegs[x].disks[0], x in [ 1,3 ])

      pegs[1].disks = (int *) malloc(sizeof(int)*(n+1));

      pegs[2].disks = (int *) malloc(sizeof(int)*(n+1));

      pegs[3].disks = (int *) malloc(sizeof(int)*(n+1));

#ifdef USE_INSTRUMENTED_CODE

      clock_t startClock,nowClock;

      int repetitions = 0;

      startClock = clock();

      do

      {

         RunOneTrial(n,pegs); nowClock = clock();

         repetitions++;

      }

      while ( ((double) (nowClock-startClock)/CLOCKS_PER_SEC) < 2.0 );

      printf("%2d disks took %10d moves (%9.3f microseconds, %10d repetitions) ",

         n,numberOfMoves,

         1E6*((double) (nowClock-startClock)/CLOCKS_PER_SEC)/repetitions,

         repetitions);

#else

      printf("%5d disks =========== ",n);

      RunOneTrial(n,pegs);

#endif

   // Deallocate peg disk space

      free(pegs[1].disks);

      free(pegs[2].disks);

      free(pegs[3].disks);

   }

  

   system("PAUSE");

   return( 0 );

}

//------------------------------------------------------------

void RunOneTrial(const int n,PEG pegs[])

//------------------------------------------------------------

{

   void DoTowersOfHanoi(const int n,const int F,const int T,const int H,PEG pegs[]);

   int i;

/*

   Initialize pegs[1] state so that pegs[1].disks[1] is the bottom disk (the largest

      disk) and pegs[1].disks[n] is the top disk (the smallest disk). *Note* In

      general, pegs[x].disks[pegs[x].size] is the top disk stored in pegs[x].

*/

   pegs[1].size = n;

   for (i = 1; i <= n; i++)

      pegs[1].disks[i] = n-i+1;

   pegs[2].size = 0;

   pegs[3].size = 0;

  

   numberOfMoves = 0;

   DoTowersOfHanoi(n,1,3,2,pegs);

}

//------------------------------------------------------------

void DoTowersOfHanoi(const int n,const int F,const int T, const int H,PEG pegs[])

//------------------------------------------------------------

{

   if (n > 0)

   {

      Student provides the single missing statement that makes a recursive call to move (n-1)

         disks from peg F to peg H while using peg T as a temporary holding spot (the auxiliary).

      if ( pegs[T].size > 0 ) assert( pegs[F].disks[pegs[F].size] < pegs[T].disks[pegs[T].size] );

#if defined( USE_INSTRUMENTED_CODE )

      numberOfMoves++;

#else

      printf("%2d from %d -> %d ",pegs[F].disks[pegs[F].size],F,T);

#endif

      pegs[T].disks[++pegs[T].size] = pegs[F].disks[pegs[F].size--];

      Student provides the single missing statement that makes a recursive call to move (n-1)

         disks from peg H to peg T while using peg F as a temporary holding spot (the auxiliary).

   }

}

Problem Complete the recursive solution to the Towers of Hanoi problem. Hint See "Example 2" on pages 73-75 in Section 2.4 of our textbook for help with the algorithm. Please fill in the missing code in the TWO designated areas in source file Problem33.c that says "Student provides missing code.. " below. PLEASE also provide screenshot of the compiled output once complete. I have provided sample ones which should give an idea on how it should look.

Explanation / Answer

This C Program utilizes recursive capacity and illuminates the pinnacle of hanoi. The pinnacle of hanoi is a numerical riddle. It comprises of threerods, and various circles of various sizes which can slideonto any bar. The riddle begins with the circles in a flawless stack in rising request of size on one pole, the littlest at the best. We need to get a similar stack on the third bar.

Here is the source code of the C program for explaining towers of hanoi. The C Program is effectively accumulated and kept running on a Linux framework.

Here is the working code

#include <stdio.h>

void towers_Hanoi(int, char, char, char);

int main()
{
int nums;

printf("Enter the number of disks : ");
scanf("%d", &nums);
printf("The sequence of moves involved in the Tower of Hanoi are : ");
towers_Hanoi(nums, 'A', 'C', 'B');
return 0;
}
void towers_Hanoi(int nums, char from_peg, char to_peg, char aux_peg)
{
if (nums == 1)
{
printf(" Move disk 1 from peg %c to peg %c", from_peg, to_peg);
return;
}
towers_Hanoi(nums - 1, from_peg, aux_peg, to_peg);
printf(" Move disk %d from peg %c to peg %c", nums, from_peg, to_peg);
towers_Hanoi(nums - 1, aux_peg, to_peg, from_peg);
}