Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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)

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribedimage text in transcribed

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

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

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

Step: 3

blur-text-image

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2018 Dublin Ireland September 10 14 2018 Proceedings Part 1 Lnai 11051

Authors: Michele Berlingerio ,Francesco Bonchi ,Thomas Gartner ,Neil Hurley ,Georgiana Ifrim

1st Edition

3030109240, 978-3030109240

More Books

Students also viewed these Databases questions

Question

=+6. What does the invisible hand of the marketplace do?

Answered: 1 week ago

Question

=+ 4. Why should policymakers think about incentives?

Answered: 1 week ago