|
|
|
|
|
|
|
// Precondition:
// All parameters are assigned && circleCount >= 0
// Postcondition:
// The values of all parameters have been printed
// && IF circleCount > 0
// circleCount circles have been moved from beginPeg to
// endPeg in the manner detailed above
// ELSE
// No further actions have taken place
{
cout << setw(6) << circleCount << setw(9) << beginPeg
<< setw(7) << auxPeg << setw(7) << endPeg << endl;
if (circleCount > 0)
{
cout << From first:;
DoTowers(circleCount -1, beginPeg, endPeg, auxPeg);
cout << setw(58) << Move circle << circleCount
<< from << beginPeg << to << endPeg << endl;
cout << From second:;
DoTowers(circleCount - 1, auxPeg, beginPeg, endPeg);
}
} |
|
|
|
|
|
|
|
|
The output from a run with three circles follows. Original means that the parameters listed beside it are from the nonrecursive call, which is the first call to DoTowers. From first: means that the parameters listed are for a call issued from the first recursive statement. From second: means that the parameters listed are for a call issued from the second recursive statement. Notice that a call cannot be issued from the second recursive statement until the preceding call from the first recursive statement has completed execution. |
|
|
|
|
|
|
|
|
OUTPUT WITH 3 CIRCLES
CALLED FROM #CIRCLES BEGIN AUXIL. END INSTRUCTIONS
Original : 3 1 2 3
From first: 2 1 3 2
From first: 1 1 2 3
From first: 0 1 3 2
Move circle 1 from 1 to 3
From second: 0 2 1 3
Move circle 2 from 1 to 2
From second: 1 3 1 2
From first: 0 3 2 1
Move circle 1 from 3 to 2
From second: 0 1 3 2 |
|
|
|
|
|