Question

In: Statistics and Probability

Code needed in C++, make changes to the file provided (18-1, has 3 files) Chapter 18...

Code needed in C++, make changes to the file provided (18-1, has 3 files)

Chapter 18 Stacks and Queues

-----------------------------------------------------------------------------------------------------

capacity is just 5

1. push 6 numbers on the stack

2. catch the overflow error in a catch block

3. pop one element, which means your capacity is now down to 4

4. push the element that was rejected earlier

5. verify your entire stack by popping to show the new numbers.

IntStack.h

#include <memory>

using namespace std;

class IntStack

{

unique_ptr<int[]>stackArray;

int capacity;

int top;

public:

// Constructor

IntStack(int capacity);

// Member functions

void push(int value);

void pop(int &value);

bool isEmpty() const;

// Stack Exceptions

class Overflow {};

class Underflow {};

};

IntStack.cpp

ZOOM

#include "intstack.h"

//************************************

// Constructor *

//************************************

IntStack::IntStack(int capacity)

{

stackArray = make_unique<int[]>(capacity);

this->capacity = capacity;

top = 0;

}

//***********************************

// Adds a value to the stack *

//***********************************

void IntStack::push(int value)

{

if (top == capacity) throw IntStack::Overflow();

stackArray[top] = value;

top++;

}

//****************************************

// Determines whether the stack is empty *

//****************************************

bool IntStack::isEmpty() const

{

return top == 0;

}

//************************************************

// Removes a value from the stack and returns it *

//************************************************

void IntStack::pop(int &value)

{

if (isEmpty()) throw IntStack::Underflow();

top--;

value = stackArray[top];

}

pr18-01.cpp

// This program illustrates the IntStack class.

#include "intstack.h"

#include <iostream>

using namespace std;

int main()

{

IntStack stack(5);

int values[] = { 5, 10, 15, 20, 25 };

int value;

cout << "Pushing...\n";

for (int k = 0; k < 5; k++)

{

cout << values[k] << " ";

stack.push(values[k]);

}

cout << "\nPopping...\n";

while (!stack.isEmpty())

{

stack.pop(value);

cout << value << " ";

}

cout << endl;

return 0;

}

Solutions

Expert Solution

/* note: use compiler std=c++14 */

/* intStack.h */

#include <memory>
using namespace std;

class IntStack

{

unique_ptr<int[]>stackArray;

int capacity;

int top;

public:

// Constructor

IntStack(int capacity);

// Member functions

void push(int value);

void pop(int &value);

bool isEmpty() const;

// Stack Exceptions

class Overflow {};

class Underflow {};

};

/* intStack.cpp */

#include "intstack.h"

//************************************

// Constructor *

//************************************

IntStack::IntStack(int capacity)

{

stackArray = make_unique<int[]>(capacity);

this->capacity = capacity;

top = 0;

}

//***********************************

// Adds a value to the stack *

//***********************************

void IntStack::push(int value)

{

if (top == capacity) throw IntStack::Overflow();

stackArray[top] = value;

top++;

}

//****************************************

// Determines whether the stack is empty *

//****************************************

bool IntStack::isEmpty() const

{

return top == 0;

}

//************************************************

// Removes a value from the stack and returns it *

//************************************************

void IntStack::pop(int &value)

{

if (isEmpty()) throw IntStack::Underflow();

top--;

value = stackArray[top];

}

/* main.cpp */

#include "intstack.h"

#include<iostream>
using namespace std;

int main()

{
   // capacity is just 5  
   IntStack stack(5);
  
   int values[] = { 5, 10, 15, 20, 25, 45 }; // make it of 6
  
   int value;
  
   cout << "Pushing...\n";
   try{
       // 1. push 6 numbers on the stack
       for (int k = 0; k < 6; k++)
       {
           cout << values[k] << " ";  
           stack.push(values[k]);
       }
   }catch(IntStack::Overflow s){
       cout<<"\n"<<"Overflow"<<endl;
   }catch(IntStack::Underflow s){
       cout<<"\n"<<"Underflow"<<endl;
   }  
   // 3. pop one element, which means your capacity is now down to 4
   stack.pop(value);
   cout<<"\nPoped one element: "<<value<<endl;
  
   // 4. push the element that was rejected earlier
   cout<<"\nPush element rejected earlier "<<endl;
   cout<<"\nPushing "<<values[5]<<endl;
   stack.push(values[5]); // values is 45 which was rejeted earlier
  
   cout << "\nPopping...\n";
   //5. verify your entire stack by popping to show the new numbers.
   while (!stack.isEmpty())
   {
       stack.pop(value);
       cout << value << " ";
   }
  
   cout << endl;

   return 0;

}

/* OUTPUT */


Related Solutions

C programming A small company provided you three files. 1) emp.txt : this file contains list...
C programming A small company provided you three files. 1) emp.txt : this file contains list of employees where each line represents data of an employee. An employee has an id (String max length 20), last name (string max length 100), and salary (int). See the example emp.txt file. 2) dept.txt: This text file contains list of departments of the employees. Each line of the file contains an employee id (string max length 20) and department name for that employee...
Please use C++. You will be provided with two files. The first file (accounts.txt) will contain...
Please use C++. You will be provided with two files. The first file (accounts.txt) will contain account numbers (10 digits) along with the account type (L:Loan and S:Savings) and their current balance. The second file (transactions.txt) will contain transactions on the accounts. It will specifically include the account number, an indication if it is a withdrawal/deposit and an amount. Both files will be pipe delimited (|) and their format will be the following: accounts.txt : File with bank account info...
Would you make separated this code by one .h file and two .c file? **********code************* #include...
Would you make separated this code by one .h file and two .c file? **********code************* #include <stdlib.h> #include <stdbool.h> #include <stdio.h> #include<time.h> // Prints out the rules of the game of "craps". void print_game_rule(void) { printf("Rules of the game of CRAPS\n"); printf("--------------------------\n"); printf("A player rolls two dice.Each die has six faces.\n"); printf("These faces contain 1, 2, 3, 4, 5, and 6 spots.\n"); printf("After the dice have come to rest, the sum of the spots\n on the two upward faces is...
Code the following in C++ and make sure to include all three files required at the...
Code the following in C++ and make sure to include all three files required at the end. Hotdogs – The World’s Greatest Cost Effective Wholesome Nutritious Food Due to world food supply scarcity and the burgeoning populations of the world’s countries, your hot stand business is globalizing to satisfy the world’s desire for a cost effective wholesome nutritious food source. Market studies have shown that tens of billions of people are craving for the eponymous hotdog which will result in...
3. Translate the following C code to MIPS assembly code (in two separate files). int main()...
3. Translate the following C code to MIPS assembly code (in two separate files). int main() { printf(“before subroutine!\n”); Subfunc(); printf(“after subroutine!\n!”); } void Subfunc() {printf(“I am subroutine!\n”);} Submission file: Lab4_3a.asm for the main routine and Lab4_3b.asm for the sub-routine.
3. Translate the following C code to MIPS assembly code (in two separate files). int main()...
3. Translate the following C code to MIPS assembly code (in two separate files). int main() { printf(“before subroutine!\n”); Subfunc(); printf(“after subroutine!\n!”); } void Subfunc() {printf(“I am subroutine!\n”);} 4. Translate the following C code to MIPS assembly (in two separate files). Run the program step by step and observe the order of instructions being executed and the value of $sp. int main() { int x=2; z=Subfunc(x); printf(“Value of z is: %d”, z); } int Subfunc(int x) { return x+1;}
Use the provided BingoBall.h and Set.h files to implement the Set.cpp file. //File: BingoBall.h #ifndef BINGOBALL_H...
Use the provided BingoBall.h and Set.h files to implement the Set.cpp file. //File: BingoBall.h #ifndef BINGOBALL_H #define   BINGOBALL_H #include <iostream> class BingoBall { public:    BingoBall():letter{'a'}, number{0} {}    BingoBall(char let, int num) :        letter{ let }, number{ num } {}    char getChar() const { return letter; }    int getNumber() const { return number; }    //overload == operator    bool operator==(BingoBall &b) const { return (number == b.getNumber() && letter == b.getChar()); } private:   ...
The files provided in the code editor to the right contain syntax and/or logic errors. In...
The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. DebugBox.java public class DebugBox { private int width; private int length; private int height; public DebugBox() { length = 1; width = 1; height = 1; } public DebugBox(int width, int length, height) { width = width; length = length; height =...
The files provided in the code editor to the right contain syntax and/or logic errors. In...
The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. Please Fix code and make Copy/Paste avaliable // Application allows user to enter a series of words // and displays them in reverse order import java.util.*; public class DebugEight4 {    public static void main(String[] args)    {       Scanner input = new Scanner(System.in);       int...
1. What is a file? 2. What are different types of files 3. What is a folder ?
1. What is a file?2. What are different types of files3. What is a folder ?4. What is File Explorer?5. What is an address bar in FIle Explorer?6. What are the 2 main ways to open a file?7. How to move a file?8. How to create a new folder?9. How to rename a file or folder?10. How to delete a file or folder?11. How to select multiple files that are next to each other?12. How to select multiple files that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT