Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with the following code. It builds and runs, but the ending conditions are whacky and I think there is an issue the

I need help with the following code. It builds and runs, but the ending conditions are whacky and I think there is an issue the moveDIsk function. Please help and thanks in advance!

// Peg.h #pragma once #include #include #include using namespace std; class Peg { private: void loadDisk(int numDisks); vector diskStack; // The stack of disks string pegName; // The name of the peg

public: Peg(string newName, int numDisks = 0); int getSize(); void setName(string newName); void printPeg(); // method to display what disks are on the peg string getName(); // accessor for the peg name int topDisk(); // method to return the value of the top disk // methods to add and remove disks from the pegs void removeDisk(); void addDisk(); int lastDisk();

// destructor ~Peg(); };

#include "Peg.h" #include #include #include #include using namespace std; Peg::Peg(string newName, int numDisks) { cout

}

void Peg::loadDisk(int numDisks) { // Load disks onto the pegs assert(numDisks >= 0); for (int i = numDisks; i > 0; i--) { diskStack.push_back(i); }

} // accessors string Peg::getName() { return pegName; }

int Peg::topDisk() // return the value of the top disk on the peg { assert(!diskStack.empty()); return diskStack.back(); } int Peg::getSize() { return diskStack.size(); }

//Mutators

void Peg::setName(string newName) { pegName = newName; }

void Peg::printPeg()

{ cout

void Peg::addDisk() { diskStack.push_back(this->getSize()); }

void Peg::removeDisk() {

return diskStack.pop_back(); } int Peg::lastDisk() { return diskStack.back();

}

//deconstructor Peg::~Peg() { cout

// File Name: Hanoi.cpp // Author: Tanner Crane // Description: Program that recursively solves the Towers of Hanoi with header file for declartions and .cpp for function definitions

#include #include #include "Peg.h" using namespace std;

// Global consts const int NUM_DISKS(1); // Number of disks to simulate

int hanoi(int numDisks, Peg& start, Peg& goal, Peg& temp); void moveDisk(Peg& from, Peg& to);

int main() { // Initialize variables Peg peg1("Peg1", NUM_DISKS), peg2("Peg2"), peg3("Peg3"); int numMoves(0);

// Introduce the program

cout

// Display the starting condition cout

// Solve Tower of Hanoi - move the disks from peg 1 to peg 3, using peg2 temporarily, getting the number of required moves returned cout

cout

cout

// Hanoi - recursive solution to the tower of hanoi problem int hanoi(int numDisks, Peg& start, Peg& goal, Peg& temp) { int numMoves(0);

// Only if there are disks to be moved if (numDisks > 0) { // First, move n-1 disks to the temporary peg, capturing the number of moves required numMoves = hanoi(numDisks - 1, start, temp, goal);

// Next, move the remaining disk to the goal peg; increment the move count cout

// Finally, move n-1 disks to the goal peg, capturing the number of moves required numMoves += hanoi(numDisks - 1, temp, goal, start); }

return(numMoves); } // Function to move a disk from one peg to another void moveDisk(Peg& from, Peg& to) { // Insure the inputs are valid: "from" has a disk, and that disk isn't smaller than the one on "to"(if there's one there). assert(from.getSize() > 0); // if (to.getSize() > 0) // { // assert(from.lastDisk()

image text in transcribed

image text in transcribed

The above are my outputs. As you can probably see, The moves required to move the disks and ending conditions aren't making sense.

- o G C:\Users\Tanner Crane source epos\hanoi Debug\hanoi.exe Welcome to Tanner's Tower of Hanoi simulator...this simulation will be running with 1 disks. Starting condition of the three pegs : Pegi has 1 disks: 1 Peg2 has 0 disks: Peg3 has 0 disks: Moves required to move 1 disks from Pegi to Peg3: Move disk 1 from Pegi to Peg3 Ending condition of the three pegs : 1 Pegi has 2 disks: 11 Peg2 has 0 disks: Pegs has 1 disks: 0 1A stack of 1 disks can be transferred in 1 moves. Thanks for using Tanner's Tower of Hanoi simulator! Press any key to continue ... - 0 x GN C:\Users\Tanner Crane source epos\hanoi Debug\hanoi.exe This is my Peg constructor This is my Peg constructor Welcome to Tanner's Tower of Hanoi simulator...this simulation will be running with 3 disks. Starting condition of the three pegs : Pegi has 3 disks: 321 arpeg2 has 0 disks: Pegs has 0 disks: Moves required to move 3 disks from Pegi to Peg3: Move disk 1 from Pegi to Peg3 Move disk 3 from Pegi to Peg2 Move disk from Pegs to Peg2 Move disk 4 from Pegi to Peg3 Move disk 1 from Peg2 to Peg1 Move disk 2 from Peg2 to Peg3 Move disk 6 from Pegi to Peg3 Ending condition of the three pegs : Pegi has 8 disks: 32134567 Peg2 has 4 disks: 0123 Pegs has 5 disks: 01234 A stack of 3 disks can be transferred in 7 moves. Thanks for using Tanner's Tower of Hanoi simulator! Press any key to continue ... 23 // Introduce the program

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

Data Visualization A Practical Introduction

Authors: Kieran Healy

1st Edition

0691181624, 978-0691181622

More Books

Students also viewed these Databases questions