Create an MS Word document which contains the code trace of program 19-10, Tower
ID: 3779581 • Letter: C
Question
Create an MS Word document which contains the code trace of program 19-10, Tower of Hanoi with 4 discs, by changing line 11 to the following:
const int NUM_DISCS = 4;
Your document must have two columns labeled line number and code trace narrative. For each line number traced/executed, you are to place the line number in a new row’s first column and complete the corresponding narrative in the second column. You are expected to use full sentences in the narrative portion. You may begin your code trace at line 11 and ends at line 20 (the int main function).
#include using namespace std;
// Function prototype void moveDiscs(int, int, int, int);
int main()
{ const int NUM_DISCS = 3; // Number of discs to move
const int FROM_PEG = 1; // Initial "from" peg
const int TO_PEG = 3; // Initial "to" peg
const int TEMP_PEG = 2; // Initial "temp" peg // Play the game.
moveDiscs(NUM_DISCS, FROM_PEG, TO_PEG, TEMP_PEG);
cout << "All the pegs are moved! "; return 0; }
void moveDiscs(int num, int fromPeg, int toPeg, int tempPeg)
{ if (num > 0)
{ moveDiscs(num 1, fromPeg, tempPeg, toPeg);
cout << "Move a disc from peg " << fromPeg << " to peg " << toPeg << endl;
moveDiscs(num 1, tempPeg, toPeg, fromPeg); } }
this is c++
write the whole code trace until all the pegs are movied
Explanation / Answer
For this code:
#include<iostream>
using namespace std;
// Function prototype
void moveDiscs(int, int, int, int);
int main()
{
const int NUM_DISCS = 4; // Number of discs to move
const int FROM_PEG = 1; // Initial "from" peg
const int TO_PEG = 3; // Initial "to" peg
const int TEMP_PEG = 2; // Initial "temp" peg // Play the game.
moveDiscs(NUM_DISCS, FROM_PEG, TO_PEG, TEMP_PEG);
cout << "All the pegs are moved! "; return 0;
}
void moveDiscs(int num, int fromPeg, int toPeg, int tempPeg)
{
if (num > 0)
{
moveDiscs(num 1, fromPeg, tempPeg, toPeg);
cout << "Move a disc from peg " << fromPeg << " to peg " << toPeg << endl;
moveDiscs(num 1, tempPeg, toPeg, fromPeg);
}
}
And that process continues, and will print the lines:
Move a disc from peg 2 to peg 3
Move a disc from peg 1 to peg 2
Move a disc from peg 3 to peg 1
Move a disc from peg 3 to peg 2
Move a disc from peg 1 to peg 2
Move a disc from peg 1 to peg 3
Move a disc from peg 2 to peg 3
Move a disc from peg 2 to peg 1
Move a disc from peg 3 to peg 1
Move a disc from peg 2 to peg 3
Move a disc from peg 1 to peg 2
Move a disc from peg 1 to peg 3
Move a disc from peg 2 to peg 3
All the pegs are moved!
Line 14 moveDiscs(4, A, B, C) is called. Line 18 moveDiscs(3, A, B, C) is called. Line 18 moveDiscs(2, A, B, C) is called. Line 18 moveDiscs(1, A, B, C) is called. Line 18 moveDiscs(0, A, B, C) is called. Line 16 Condition fails. Line 19 Prints text move a disc from peg 1 to peg 2. Line 20 moveDiscs(0, B, C, A) is called. Line 19 Prints text move a disc from peg 1 to peg 3.Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.