C Programming Only. (pgs 73-75 of section 2.4 included below) //----------------
ID: 3750195 • 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
The objective of this is to move all rings from 1 peg to 3 peg. You can move only 1 ring at a time. If there are more than rings as an intermediate step you move n-1 rings from peg 1 to peg 2. If this involves moving more than 1 ring you use peg 3 as an intermediate. So the code to be added in the missing line is
DoTowersOfHanoi(n-1,1,3,2)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.