Question: C Programming Only. (pgs 73-75 of section 2.4 included below) //------------------------------------------------------------ // Problem #33 // Problem33.c //------------------------------------------------------------ #include #include #include #include #include #define USE_INSTRUMENTED_CODE int

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

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

Problem #33 // Problem33.c //------------------------------------------------------------ #include #include #include #include #include #define USE_INSTRUMENTED_CODE

int numberOfMoves; //------------------------------------------------------------ typedef struct //------------------------------------------------------------ { int size; int *disks; }

PEG; //------------------------------------------------------------ int main() //------------------------------------------------------------ { void RunOneTrial(const int n,PEG pegs[]); intLB,UB,n; PEG pegs[3+1]; printf("LB? "); scanf("%d",&LB); printf("UB? "); scanf("%d",&UB); for (n =

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

// Problem #33

// Problem33.c

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

#include

#include

#include

#include

#include

#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

{

// 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)

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

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]

#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. 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

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!