Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Specify, design, and implement a class that keeps track of rings stacked on a pe

ID: 440247 • Letter: S

Question

Specify, design, and implement a class that keeps track of rings stacked on a peg, rather like phonograph records on a spindle. An example with five rings is shown here: The peg may hold up to 64 rings, with each ring having its own diameter. Also, there is a rule that requires each ring to be smaller than any ring underneath it, as shown in our example. The class's member functions should include: (a) a constructor that places n rings on the peg (where n may be as large as 64); use 64 for a default argument. These n rings have diameters from n inches (on the bottom) to 1-inch (on the top). (b) a constant member function that returns the number of rings on the peg. (c) a constant member function that returns the diameter of the topmost ring. (d) a member function that adds a new ring to the top (with the diameter of the ring as a parameter to the function). (e) a member function that removes the topmost ring. (f) an overloaded output function that prints some clever representation of the peg and its rings. Make sure that all functions have appropriate preconditions to guarantee that the rule about ring sizes is enforced. Also spend time designing appropriate private data fields.

Explanation / Answer

#include
using namespace std;
void hanoi( char peg1, char peg2, char peg3, int how_many );
int main()
{
char peg1 = ’A’;// origin
peg2 = ’B’;// destination
peg3 = ’C’;// spare
int how_many; // number of disks initially on the origin
// Prompt for and warn about
// the number of disks to be moved.
cout << " How many disks initially on peg A?"<< " If more than 7, the solution may seem to "<< "take forever! ";
cin >> how_many;
cout << endl << endl;
// Anything to move?
if ( how_many < 1 )
cout << "There’s nothing to move!" << endl;
// Otherwise solve with://-- peg1 as the origin//-- peg2 as the destination//-- peg3 as the spare
else
hanoi( peg1, peg2, peg3, how_many );
return 0;
}
// p1 -- origin// p2 -- destination// p3 -- spare// how_many -- number of disks to move
void hanoi( char p1, char p2, char p3, int how_many )
{
// If there is only 1 disk on p1, then move it to p2
// and quit as the problem is solved.
if ( how_many == 1 )
{
cout << "Move top disk from peg " << p1<< " to peg " << p2 << endl;
return;
}// Otherwise:
//(1) Move how_many - 1 disks from p1 to p3:
//p1 is the origin, p3 is the destination,
//and p2 is the spare.
////(2) Move the top disk from p1 to p2.
////(3) Move how_many - 1 disks from p3 to p2:
//p3 is the origin, p2 is the destination,
//and p1 is the spare.
hanoi( p1, p3, p2, how_many - 1 );
cout << "Move top disk from peg " << p1<< " to peg " << p2 << endl;
hanoi( p3, p2, p1, how_many - 1 );
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote