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

Concept explainers

Question
Book Icon
Chapter 19, Problem 7PC
Program Plan Intro

Dynamic MathStack

Program Plan:

MathStack.h:

  • Include required header files
  • Declare a class named “MathStack”. Inside the class,
    • Inside “public” access specifier,
      • Declare functions “add ()”, “sub ()”, “mult ()”, “div ()”, “addAll ()”, and “multAll ()”.

MathStack.cpp:

  • Include required header files.
  • Give function definition to add elements “add()”.
    • Declare required variables “number”, and “sum_Value”.
    • Call the function “pop()”.
    • Add the elements.
    • Push the value into the stack using the function “push()”.
  • Give function definition to subtract elements “sub()”.
    • Declare required variables “number”, and “diff_Value”.
    • Call the function “pop()”.
    • Subtract the elements.
    • Push the value into the stack using the function “push()”.
  • Give function definition to multiply elements “mult()”.
    • Declare required variables “number”, and “prod_Value”.
    • Call the function “pop()”.
    • Multiply the elements.
    • Push the value into the stack using the function “push ()”.
  • Give function definition to divide elements “div()”.
    • Declare required variables “number”, and “quo_Value”.
    • Call the function “pop()”.
    • Divide the elements.
    • Push the value into the stack using the function “push()”.
  • Give function definition to add all the elements “addAll()”.
    • Declare required variables “number”, and “sum_Value”.
    • Call the function “pop()”.
    • Add all the elements.
    • Push the value into the stack using the function “push()”.
  • Give function definition to multiply all the elements “multAll()”.
    • Declare required variables “number”, and “prod_Value”.
    • Call the function “pop()”.
    • Multiply all the elements.
    • Push the value into the stack using the function “push()”.

IntStack.h:

  • Include required files.
  • Declare a class named “IntStack”. Inside the class,
    • Inside “protected” access specifier,
      • Declare a pointer named “stackArray”.
      • Declare variables “stackSize” and “top”.
    • Inside “public” access specifier,
      • Declare constructor and destructor.
      • Give function declarations.

IntStack.cpp:

  • Declare required header files.
  • Give definition for constructor,
    • Create a stack array and assign the size
  • Give definition for destructor,
    • Delete the array and assign it to null
  • Give function definition to push elements “push()”
    •  Check if the stack is full using the function “isFull()”,
      • If the condition is true then, print “The stack is full”.
      • If the condition is not true then,
        • Increment the variable.
        • Assign “num” to the top position.
  • Give function definition to pop elements “pop ()”,
    • Check if the stack is empty using the function “isEmpty()”.
      • If the condition is true then, print “The stack is empty”.
        • Assign top element to the variable.
        • Decrement the variable.
  • Give function definition to check if the stack is full “isFull()”.
    • Assign “false” to the Boolean variable “status”.
    • Check if the stack size if full.
      • Assign true
    • Return the Boolean variable “status”.
  • Give function definition to check if the stack is empty “isEmpty()”.
    • Assign “false” to the Boolean variable
    • Check if top is empty
      • Assign true.
    • Return the variable

Main.cpp:

  • Include required header files.
  • Inside “main ()” function,
    • Declare constant variables “STACKSIZE”, “ADDSIZE”, and “MULTSIZE”.
    • Create three stacks “stack”, “addAllStack”, and “multAllStack”.
    • Declare a variable “popVar”.
    • 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
Data structure & Algorithum java program 1) Add a constructor to the class "AList" that creates a list from a given array of objects.2) Add a method "getPosition" to the "AList" class that returns the position of a given object in the list. The header of the method is as follows : public int getPosition(T anObject)3) Write a program that thoroughly tests all the methods in the class "AList".4) Include comments to all source code.
Term by CodeChum Admin (JAVA CODE) Construct a class called Term. It is going to represent a term in polynomial expression. It has an integer coefficient and an exponent. In this case, there is only 1 independent variable that is 'x'.   There should be two operations for the Term: public Term times(Term t) - multiplies the term with another term and returns the result public String toString() - prints the coefficient followed by "x^" and appended by the exponent. But with the following additional rules: if the coefficient is 1, then it is not printed. if the exponent is 1, then it is not printed ( the caret is not printed as well) if the exponent is 0, then only the coefficient is printed.  Input The first line contains the coefficient and the exponent of the first term. The second line contains the coefficient and the exponent of the second term. 1·1 4·3 Output Display the resulting product for each of the test case. 4x^4
Data Structure & Algorithum java program Do the following: 1) Add a constructor to the class "LList" that creates a list from a given array of objects.2) Add a method "addAll" to the "LList" class that adds an array of items to the end of the list. The header of the method is as follows, where "T" is the generic type of the objects in the list. 3) Write a Test/Driver program that thoroughly tests all the methods in the class "LList".

Chapter 19 Solutions

Starting Out with C++ from Control Structures to Objects (9th 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
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage