Question
Purpose The purpose of this is to test your knowledge of the concepts learned in this course so far: Functions Arrays Adding or removing, and
Purpose
The purpose of this is to test your knowledge of the concepts learned in this course so far:
Functions
Arrays
Adding or removing, and shifting elements in an array
Task
Open the Algorithmic Design Document, make a copy, and follow the steps to create your algorithm.
You must express your algorithm as pseudocode or a flowchart.
Please take a look at the sample run below before you continue
You are not allowed to use
Complete the functions pop(). The function printStack()has been created for you. Please use printStack() to print the stack.
Complete the function: int pop(int stack[], int &size, int val)
Removes a value out of a stack of integers.
Integers are removed from the top of the stack.
If the integer is in the stack, ALL integers on the top are also removed and the remaining elements are shifted to the top of the stack. Check the sample run very carefully so you understand how to remove it.
The first occurrence is removed if there are multiple occurrences.
Takes three arguments:
stack The array containing the stack
size A reference to the number of elements in the stack
val The integer to be removed from the stack
Returns 0 on success, 1 on underflow (stack is empty, no values to pop), 2 if integer is not in the stack.
For example, a stack with MAX of 5 and size 3, after integers 8, -2, 3 were added in that order is shown below.
Using the stack above, the stack after a call to pop(stack, size, -2), notice both elements -2 and 3 were removed from the stack, size is now 1:
Requirements for the pop() function:
Do not alter the existing prototypes.
Your function should remove the integers from the TOP of the stack. TOP of the stack is at index 0.
If an overflow or an underflow occurs, your functions must return an error code; they should not attempt to modify the stack. Handle the return code in the main() function, print the error message in main().
pop should only manipulate the given stack; they should not print anything!
In your main(), an array of size 3 has been declared for you. Using a small array will make it easier to debug all of the conditions. You should then read user commands through a menu (input type char) and use the stack functions to manipulate the stack accordingly. The program must support the following single-character commands:
Removes an integer from the top of the stack. All integers on top are also removed (see sample run).
p Prints the stack.
q Quit.
The stack should be printed using printStack() after every command except q.
If an unknown command is entered, print an error message and DO NOT print the stack.
Use only the concepts we have learned so far.
C++ no vector header no string header no stack header no header besides iostream
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started