#include "stdafx.h"
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
// function prototypes
int loadDisk(vector<int> &firstPeg, int numDisks);
void printPeg(vector<int> &peg);
int hanoi(struct pegType &startPeg, struct pegType
&swapPeg, struct pegType &endPeg, int numDisk);
void moveDisk(struct pegType &startPeg, struct pegType
&endPeg);
struct pegType
{
vector <int> diskStack;
int name;
};
int main()
{
int numDisk = 7;
int i;
pegType peg1, peg2, peg3;
peg1.name = 1;
peg2.name = 2;
peg3.name = 3;
cout << "-------------------------------"
<< endl;
cout << " Towers of Hanoi " << endl;
cout << "-------------------------------"
<< endl << endl;
loadDisk(peg1.diskStack, numDisk); // Load Peg 1
with 7 disks
cout << "\npeg1 has " <<
peg1.diskStack.size() << " discs";
cout << "\npeg2 has 0" << " discs";
cout << "\npeg3 has 0" << " discs"
<< endl;
cout << "Starting Condition: " << endl
<< endl;
printPeg(peg1.diskStack);
printPeg(peg2.diskStack);
printPeg(peg3.diskStack);
int total = hanoi(peg1, peg3, peg2, numDisk);
cout << "End Condition: " << endl
<< endl;
printPeg(peg1.diskStack);
printPeg(peg2.diskStack);
printPeg(peg3.diskStack);
cout << "A stack of " << numDisk
<< " can be transferred in " << total << "
moves!" << endl;
system("pause");
return 0;
}
// function definitions
int loadDisk(vector<int> &pegType, int numDisks)
{
int i;
for (i = numDisks; i > 0; i--)
{
pegType.push_back(i);
}
return i;
}
void printPeg(vector<int> &peg)
{
for (unsigned int i = 0; i < peg.size(); i++)
{
cout << peg[i] <<
endl;
}
}
int hanoi(pegType &startPeg, pegType &swapPeg, pegType
&endPeg, int numDisk)
{
int totalMoves = 0;
if (numDisk > 0)
{
totalMoves = hanoi(startPeg,
endPeg, swapPeg, numDisk-1);
moveDisk(startPeg, endPeg);
totalMoves += hanoi(startPeg,
endPeg, swapPeg, numDisk-1); // count total moves
}
return totalMoves;
}
void moveDisk(pegType &startPeg, pegType &endPeg)
{
assert(startPeg.diskStack.size() > 0); // makes
sure that there is something on the peg
if (endPeg.diskStack.size() > 0)
{
assert(startPeg.diskStack.back()
< endPeg.diskStack.back()); // make sure that the start peg has
less than goal peg
}
startPeg.diskStack.pop_back();
int n = startPeg.diskStack.back();
cout << "\nMove Disk " << n << "
from peg" << startPeg.name << " to peg" <<
endPeg.name;
endPeg.diskStack.push_back(n);
}
PLEASE HELP ME FIGURE OUT WHY MY ASSERT DOESNT WORK! // assert(startPeg.diskStack.back() < endPeg.diskStack.back()); THIS LINE IS WHEN IT ENDS

#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
// function prototypes
int loadDisk(vector<int> &firstPeg, int numDisks);
void printPeg(vector<int> &peg);
int hanoi(struct pegType &startPeg, struct pegType &swapPeg, struct pegType &endPeg, int numDisk);
void moveDisk(struct pegType &startPeg, struct pegType &endPeg);
struct pegType
{
vector <int> diskStack;
int name;
};
int main()
{
int numDisk = 7;
int i;
pegType peg1, peg2, peg3;
peg1.name = 1;
peg2.name = 2;
peg3.name = 3;
cout << "-------------------------------" << endl;
cout << " Towers of Hanoi " << endl;
cout << "-------------------------------" << endl << endl;
loadDisk(peg1.diskStack, numDisk); // Load Peg 1 with 7 disks
cout << "
peg1 has " << peg1.diskStack.size() << " discs";
cout << "
peg2 has 0" << " discs";
cout << "
peg3 has 0" << " discs" << endl;
cout << "Starting Condition: " << endl << endl;
printPeg(peg1.diskStack);
printPeg(peg2.diskStack);
printPeg(peg3.diskStack);
int total = hanoi(peg1, peg3, peg2, numDisk);
cout << "End Condition: " << endl << endl;
printPeg(peg1.diskStack);
printPeg(peg2.diskStack);
printPeg(peg3.diskStack);
cout << "A stack of " << numDisk << " can be transferred in " << total << " moves!" << endl;
system("pause");
return 0;
}
// function definitions
int loadDisk(vector<int> &pegType, int numDisks)
{
int i;
for (i = numDisks; i > 0; i--)
{
pegType.push_back(i);
}
return i;
}
void printPeg(vector<int> &peg)
{
for (unsigned int i = 0; i < peg.size(); i++)
{
cout << peg[i] << endl;
}
}
int hanoi(pegType &startPeg, pegType &swapPeg, pegType &endPeg, int numDisk)
{
int totalMoves = 0;
if (numDisk > 0)
{
if(numDisk != 1)
totalMoves = hanoi(startPeg, endPeg, swapPeg, numDisk-1);
moveDisk(startPeg, endPeg);
totalMoves += 1;
if(numDisk != 1)
totalMoves += hanoi(swapPeg, startPeg, endPeg, numDisk-1); // count total moves
}
return totalMoves;
}
void moveDisk(pegType &startPeg, pegType &endPeg)
{
assert(startPeg.diskStack.size() > 0); // makes sure that there is something on the peg
if (endPeg.diskStack.size() > 0)
{
assert(startPeg.diskStack.back() < endPeg.diskStack.back()); // make sure that the start peg has less than goal peg
}
int n = startPeg.diskStack.back();
startPeg.diskStack.pop_back();
cout << "
Move Disk " << n << " from peg" << startPeg.name << " to peg" << endPeg.name;
endPeg.diskStack.push_back(n);
}
=============
You were first popping, and then taking the backvalue.. which is
wrong.. first take back value and then pop.. i have corrected the
code above.. Please upvote..

#include "stdafx.h" #include <iostream> #include <vector> #include <cassert> using namespace std; // function prototypes int loadDisk(vector<int>...
#include "stdafx.h" #include <iostream> using namespace std; class dateType { private: int dmonth; int dday; int dyear; public: void setdate (int month, int day, int year); int getday()const; int getmonth()const; int getyear()const; int printdate()const; bool isleapyear(int year); dateType (int month=0, int day=0, int year=0); }; void dateType::setdate(int month, int day, int year) { int numofdays; if (year<=2008) { dyear=year;...
#include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...
vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector { public: Vector( int initsize = 0 ) : theSize( initsize ), theCapacity( initsize + SPARE_CAPACITY ) { objects = new T[ theCapacity ]; } Vector( const Vector & rhs ) : theSize( rhs.theSize), theCapacity( rhs.theCapacity ), objects( 0 ) { objects = new T[ theCapacity ]; for( int k = 0; k < theSize; ++k) objects[ k ] = rhs.objects[ k...
#include <iostream> #include <vector> #include <iomanip> using namespace std; int main() { const int NUM_ITEMS = 8; vector <double> inverse(NUM_ITEMS); int j; double temp; for (int i = 0; i < NUM_ITEMS; i++) { inverse.at(i) = 1 / (i + 1.0); } cout << fixed << setprecision(2); cout << "Original vector..." << endl; for (int i = 0; i < NUM_ITEMS; i++) { cout << inverse.at(i) << " "; } cout << endl; cout << "Reversed vector..." << endl; for...
CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...
#include <cstring> // for strlen() #include <cassert> // for assert() #include <iostream> // for cout using namespace std; class MyString { private: char *m_data; int m_length; public: MyString(const char *source = "") { assert(source); // make sure source isn't a null string // Find the length of the string // Plus one character for a terminator ...
C++ Implement the removeBad function: #include <list> #include <vector> #include <algorithm> #include <iostream> #include <cassert> using namespace std; vector<int> destroyedOnes; class Movie { public: Movie(int r) : m_rating(r) {} ~Movie() { destroyedOnes.push_back(m_rating); } int rating() const { return m_rating; } private: int m_rating; }; // Remove the movies in li with a rating below 50 and destroy them. // It is acceptable if the order of the remaining movies is not // the same as in the original list. void...
#include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> smallerNumbersThanCurrent(vector<int>& nums) { int N = nums.size(); vector<int> result; vector<int> a(101); vector<int> b(101); for (int i = 0; i < N; i++) { a[nums[i]]++; // what does this mean? } for (int i = 1; i < 101; i++) { b[i] = a[i - 1] + b[i - 1]; } for (int i = 0; i < N; i++) { result.push_back(b[nums[i]]); } for (int i = 0; i...
#include <iostream> #include <cstdlib> using namespace std; int **dynArray(int row, int cols) { int **myPtr; int lab[4]; myPtr = new int *[row]; for(int i = 0; i < row; i++) myPtr[i] = new int[lab[i]]; for(int i = 0; i<row ; i++) if(myPtr[i] == 0) cout<<"empty"; return myPtr; } void getinput(int ID,int &Station,int &labnumb) { cout<<" Enter your ID number: "<<endl; cin>>ID; cout<<" Enter your station number: "<<endl; cin>>Station; cout<<" Enter your lab number: "<<endl; cin>>labnumb; return; } void logout(int ID,int...
#include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() { char string[100]; char inputChoice, choice[2]; int vowelTotal, consonantTotal; //Input a string cout << "Enter a string: " << endl; cin.getline(string, 100); do { //Displays the Menu cout << " (A) Count the number of vowels in the string"<<endl; cout << " (B) Count...