Starting Out with C++ from Control Structures to Objects (8th Edition)
Starting Out with C++ from Control Structures to Objects (8th Edition)
8th Edition
ISBN: 9780133769395
Author: Tony Gaddis
Publisher: PEARSON
bartleby

Concept explainers

Question
Book Icon
Chapter 18, Problem 8PC
Program Plan Intro

Dynamic MathStack Template

Program Plan:

MathStack.h:

  • Include required header files
  • Declare a class named “MathStack” which inherits “DynStack” Inside the class,
    • Inside “public” access specifier,
      • Declare functions “add ()”, “sub ()”, “mult ()”, “div ()”, “addAll ()”, and “multAll ()”.
  • Declare class template.
  • Give function definition to add elements “add ()”.
    • Declare required template variables “num_Value”, and “sum_Value”.
    • Call the function “pop ()”
    • Add the elements.
    • Push the value into the stack using the function “push ()”.
  • Declare class template.
  • Give function definition to subtract elements “sub ()”.
    • Declare required template variables “num_Value”, and “diff_Value”.
    • Call the function “pop ()”
    • Subtract the elements.
    • Push the value into the stack using the function “push ()”.
  • Declare class template.
  • Give function definition to multiply elements “mult ()”.
    • Declare required template variables “num_Value”, and “prod_Value”.
    • Call the function “pop ()”
    • Multiply the elements.
    • Push the value into the stack using the function “push ()”.
  • Declare class template.
  • Give function definition to divide elements “div ()”.
    • Declare required template variables “num_Value”, and “quo_Value”.
    • Call the function “pop ()”
    • Divide the elements.
    • Push the value into the stack using the function “push ()”.
  • Declare class template.
  • Give function definition to add all the elements “addAll ()”.
    • Declare required template variables “num_Value”, and “sum_Value”.
    • Call the function “pop ()”
    • Add all the elements.
    • Push the value into the stack using the function “push ()”.
  • Declare class template.
  • Give function definition to multiply all the elements “multAll ()”.
    • Declare required template variables “num_Value”, and “prod_Value”.
    • Call the function “pop ()”
    • Multiply all the elements.
    • Push the value into the stack using the function “push ()”.

DynStack.h:

  • Include required header files.
  • Create a template.
  • Declare a class named “DynStack”. Inside the class
    • Inside the “protected” access specifier,
      • Give structure declaration for the stack
        • Create an object for the template
        • Create a stack pointer name “next”.
      • Create a stack pointer name “top”
      • Declare a variable named “stackSize”.
    • Inside the “public” access specifier,
      • Give a declaration for a constructor.
        • Assign null to the top node.
      • Give function declaration for “push ()”, “pop ()”,and “isEmpty ()”.
  • Give the class template.
  • Give function definition for “push ()”.
    • Assign null to the new node.
    • Dynamically allocate memory for new node
    • Assign “num” to the value of new node.
    • Check if the stack is empty using the function “isEmpty ()”
      • If the condition is true then assign new node as the top and make the next node as null.
      • If the condition is not true then, assign top node to the next of new node and assign new node as the top.
  • Give the class template.
  • Give function definition for “pop ()”.
    • Assign null to the temp node.
    • Check if the stack is empty using the function “is_Empty ()”
      • If the condition is true then print “The stack is empty”.
      • If the condition is not true then,
        • Assign top value to the variable “num”.
        • Link top of next node to temp node.
        • Delete the top node and make temp as the top node.
  • Give the class template.
  • Give function definition for “isEmpty ()”.
    • Assign false to a Boolean variable
    • Check if the top node is null
      • Assign true to “status”.
    • Return the status

Main.cpp:

  • Include required header files.
  • Inside “main ()” function,
    • Declare constant variables “STACKSIZE”, “ADDSIZE”, and “MULTSIZE”.
    • Create three stacks “stack”, “addAllStack”, and “multAllStack”.
    • Declare two variables “popVar” and “ipopVar”.
    • Push two elements to perform add operation.
    • Call the function “add ()”.
    • Display the element.
    • Push two elements to perform multiplication operation.
    • Call the function “mult ()”.
    • Display the element.
    • Push two elements to perform division operation.
    • Call the function “div ()”.
    • Display the element.
    • Push two elements to perform subtraction operation.
    • Call the function “sub ()”.
    • Display the element.
    • Push four elements to perform addAll operation.
    • Call the function “addAll ()”.
    • Display the element.
    • Push six elements to perform multAll operation.
    • Call the function “multAll ()”.
    • Display the element.

Blurred answer
Students have asked these similar questions
Change the following code so that there is always at least one way to get from the left corner to the top right, but the labyrinth is still randomized. The player starts at the bottom left corner of the labyrinth. He has to get to the top right corner of the labyrinth as fast he can, avoiding a meeting with the evil dragon. Take care that the player and the dragon cannot start off on walls. Also the dragon starts off from a randomly chosen position   public class Labyrinth {    private final int size;    private final Cell[][] grid;     public Labyrinth(int size) {        this.size = size;        this.grid = new Cell[size][size];        generateLabyrinth();    }     private void generateLabyrinth() {        Random rand = new Random();        for (int i = 0; i < size; i++) {            for (int j = 0; j < size; j++) {                // Randomly create walls and paths                grid[i][j] = new Cell(rand.nextBoolean());            }        }        // Ensure start and end are…
Change the following code so that it checks the following 3 conditions: 1. there is no space between each cells (imgs) 2. even if it is resized, the components wouldn't disappear 3. The GameGUI JPanel takes all the JFrame space, so that there shouldn't be extra space appearing in the frame other than the game.   Main():         Labyrinth labyrinth = new Labyrinth(10);         Player player = new Player(9, 0);        Dragon dragon = new Dragon(9, 9);         JFrame frame = new JFrame("Labyrinth Game");        GameGUI gui = new GameGUI(labyrinth, player, dragon);         frame.add(gui);        frame.setSize(600, 600);        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        frame.setVisible(true);   public class GameGUI extends JPanel {    private final Labyrinth labyrinth;    private final Player player;    private final Dragon dragon; //labyrinth, player, dragon are just public classes     private final ImageIcon playerIcon = new ImageIcon("data/images/player.png");…
Make the following game user friendly with GUI, with some simple graphics. The GUI should be in another seperate class, with some ImageIcon, and Game class should be added into the pane. The following code works as this: The objective of the player is to escape from this labyrinth. The player starts at the bottom left corner of the labyrinth. He has to get to the top right corner of the labyrinth as fast he can, avoiding a meeting with the evil dragon. The player can move only in four directions: left, right, up or down. There are several escape paths in all labyrinths. The player’s character should be able to moved with the well known WASD keyboard buttons. If the dragon gets to a neighboring field of the player, then the player dies. Because it is dark in the labyrinth, the player can see only the neighboring fields at a distance of 3 units.  Cell Class: public class Cell { private boolean isWall; public Cell(boolean isWall) { this.isWall = isWall; } public boolean isWall() { return…

Chapter 18 Solutions

Starting Out with C++ from Control Structures to Objects (8th Edition)

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education