Question: In C++, Dynamic MathStack The MathStack class shown in this chapter only has two member functions: add and sub . Write the following additional member
In C++,
Dynamic MathStack
The MathStack class shown in this chapter only has two member functions: add and
sub . Write the following additional member functions:
Function Description
mult - Pops the top two values off the stack, multiplies them, and pushes
their product onto the stack.
div - Pops the top two values off the stack, divides the second value by
the first, and pushes the quotient onto the stack.
addAll - Pops all values off the stack, adds them, and pushes their sum
onto the stack.
multAll -Pops all values off the stack, multiplies them, and pushes their
product onto the stack.
Demonstrate the class with a driver program.
Need help! One of the experts helped me with this programming assignment but there are still errors. What I highlighted in black are all the errors that need to be fixed in my program.
// File Name: MathStackMain.cpp
// This program demonstrates the MathStack class. #include
int main() { int catchVar; // To hold values popped off the stack
// Create a MathStack object. MathStack stack(5);
// Push 3 and 6 onto the stack. cout << "Pushing 3 "; stack.push(3); cout << "Pushing 6 "; stack.push(6);
// Add the two values. stack.add();
// Pop the sum off the stack and display it. cout << "The sum is "; stack.pop(catchVar); cout << catchVar << endl << endl;
// Push 7 and 10 onto the stack cout << "Pushing 7 "; stack.push(7); cout << "Pushing 10 "; stack.push(10);
// Subtract 7 from 10. stack.sub();
// Pop the difference off the stack and display it. cout << "The difference is "; stack.pop(catchVar); cout << catchVar << endl<
// Push 2 and 3 onto the stack cout << "Pushing 2 "; stack.push(2); cout << "Pushing 3 "; stack.push(3);
// Multiply 2 with 3. stack.mult();
// Pop the product off the stack and display it. cout << " The Product is "; stack.pop(catchVar); cout << catchVar << endl<
// Push 2 and 10 onto the stack cout << "Pushing 2 "; stack.push(2); cout << "Pushing 10 "; stack.push(10);
// Divide 10 by 2. stack.div();
// Pop the quotient off the stack and display it. cout << " The Quotient is "; stack.pop(catchVar); cout << catchVar << endl<
// Push 2, 5, 7, 3, 1 onto the stack cout << "Pushing 2 "; stack.push(2); cout << "Pushing 5 "; stack.push(5); cout << "Pushing 7 "; stack.push(7); cout << "Pushing 3 "; stack.push(3); cout << "Pushing 1 "; stack.push(1);
// Add all the values in the stack stack.addAll();
// Pop the sum off the stack and display it. cout << " Sum of all the elements in the stack "; stack.pop(catchVar); cout << catchVar << endl<
// Push 2, 5, 7, 3, 1 onto the stack cout << "Pushing 2 "; stack.push(2); cout << "Pushing 5 "; stack.push(5); cout << "Pushing 7 "; stack.push(7); cout << "Pushing 3 "; stack.push(3); cout << "Pushing 1 "; stack.push(1);
// Multiply all the values in the stack stack.multAll();
// Pop the product off the stack and display it. cout << " Product of all the elements in the stack "; stack.pop(catchVar); cout << catchVar << endl; return 0; }
Sample Output:
Pushing 3 Pushing 6 The sum is 9
Pushing 7 Pushing 10 The difference is 3
Pushing 2 Pushing 3
The Product is 6
Pushing 2 Pushing 10
The Quotient is 5
Pushing 2 Pushing 5 Pushing 7 Pushing 3 Pushing 1
Sum of all the elements in the stack 18
Pushing 2 Pushing 5 Pushing 7 Pushing 3 Pushing 1
Product of all the elements in the stack 210
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
