Question

In: Computer Science

sing arrays please write a program to implement the STACK concept after creating the Array, the...

sing arrays please write a program to implement the STACK concept

after creating the Array, the user is to be presented with a menu to choose from a number of options such as pop, push, top, etc...

elements to be added on the stack are ints between 0 and 99

include a loop to re display the options (menu)

and an outer loop to restart the program

Write a C++ program to implement the Stacks concept.

the stack implementation is to perform the following operations

create_Stack()

Push (s,i)

Pop(s,i)

Top(s)

Purge(s)

Extra option to display the entire stack()

when the user runs the program he should be presented with the following menu

Stack is empty

----------Menu----------

1: Create a new stack

2: Push a value

3: Pop the value

4: Display the top value

5: Purge the stack

6: Display the whole stack

0: Exit the program

submit:

1) design puse (pseudocode)

2) source code (cpp file)

3) an output of ALLLLL scenarios  

Solutions

Expert Solution

Here is the program:

#include <iostream>
using namespace std;

void create_Stack (int s[], int &i) {
    i = 0;
}

void Push (int s[], int &i) {
    if (i == 100)
        cout << "Stack full, cannot push any more onto stack.\n";
    else {
        cout << "Please enter integer to push on stack: ";
        cin >> s [i++];
    }
}

void Pop (int s[], int &i) {
    if (i == 0)
        cout << "No elements in stack to pop.\n";
    else 
        cout << "Popped from stack: " << s [--i] << endl;
}

void Top (int s[], int &i) {
    if (i == 0)
        cout << "No elements in stack.\n";
    else 
        cout << "Element at top of stack is: " << s [i - 1] << endl;
}

void Purge (int s[], int &i) {
    i = 0;
    cout << "Stack contents purged, stack now empty.\n";
}

void Display (int s     [], int &i) {
    cout << "Stack contents: ";
    for (int j = 0; j < i; j++)
        cout  << s[j] << " " << endl;
 }

int main()
{
    int myStack[100], depth = 0, choice = -1;
    char onceMore;

    do {
        create_Stack (myStack, depth);

        while (choice != 0) {
            cout<<" (1):Create a new stack\n ";
            cout<<"(2):Push a value\n ";
            cout<<"(3):Pop the value\n ";
            cout<<"(4):Display the top value\n ";
            cout<<"(5):Purge the stack\n ";
            cout<<"(6):Display the whole stack\n ";
            cout<<"(0):Exit\n ";
            cout<<"Please enter your choice: ";
            cin >> choice;
            switch (choice) {
                case 1:
                    create_Stack (myStack, depth);
                    break;
                case 2:
                    Push (myStack, depth);
                    break;
                case 3:
                    Pop (myStack, depth);
                    break;
                case 4:
                    Top (myStack, depth);
                    break;
                case 5:
                    Purge (myStack, depth);
                    break;
                case 6:
                    Display (myStack, depth);
                    break;
            }
        }
        cout << "Restart the stack program? (y/n):";
        cin >> onceMore;
        choice = -1;
    } while (onceMore == 'y');
    
    return 0;
}

Here is sample output:

 (1):Create a new stack
 (2):Push a value
 (3):Pop the value
 (4):Display the top value
 (5):Purge the stack
 (6):Display the whole stack
 (0):Exit
 Please enter your choice: 1
 (1):Create a new stack
 (2):Push a value
 (3):Pop the value
 (4):Display the top value
 (5):Purge the stack
 (6):Display the whole stack
 (0):Exit
 Please enter your choice: 2
Please enter integer to push on stack: 23
 (1):Create a new stack
 (2):Push a value
 (3):Pop the value
 (4):Display the top value
 (5):Purge the stack
 (6):Display the whole stack
 (0):Exit
 Please enter your choice: 2
Please enter integer to push on stack: 45
 (1):Create a new stack
 (2):Push a value
 (3):Pop the value
 (4):Display the top value
 (5):Purge the stack
 (6):Display the whole stack
 (0):Exit
 Please enter your choice: 4
Element at top of stack is: 45
 (1):Create a new stack
 (2):Push a value
 (3):Pop the value
 (4):Display the top value
 (5):Purge the stack
 (6):Display the whole stack
 (0):Exit
 Please enter your choice: 6
Stack contents: 23 
45 
 (1):Create a new stack
 (2):Push a value
 (3):Pop the value
 (4):Display the top value
 (5):Purge the stack
 (6):Display the whole stack
 (0):Exit
 Please enter your choice: 2
Please enter integer to push on stack: 23
 (1):Create a new stack
 (2):Push a value
 (3):Pop the value
 (4):Display the top value
 (5):Purge the stack
 (6):Display the whole stack
 (0):Exit
 Please enter your choice: 2
Please enter integer to push on stack: 66
 (1):Create a new stack
 (2):Push a value
 (3):Pop the value
 (4):Display the top value
 (5):Purge the stack
 (6):Display the whole stack
 (0):Exit
 Please enter your choice: 6
Stack contents: 23 
45 
23 
66 
 (1):Create a new stack
 (2):Push a value
 (3):Pop the value
 (4):Display the top value
 (5):Purge the stack
 (6):Display the whole stack
 (0):Exit
 Please enter your choice: 5
Stack contents purged, stack now empty.
 (1):Create a new stack
 (2):Push a value
 (3):Pop the value
 (4):Display the top value
 (5):Purge the stack
 (6):Display the whole stack
 (0):Exit
 Please enter your choice: 0
Restart the stack program? (y/n):n


Pseudo code:

Pseudo code:
-----------
while user wants to continue to use:
   Create a new stack
   while user wants to perform operations on the stack:
       Present menu options to user
       Based on menu options, call Pop, Push, Display, Top, Purge functions
       Display output corresponding to the actions
      
Exit

Pop:
   If stack is not empty:
       Remove top element and reduce stack size
      
Push:
   If stack is not full:
       Add new element at top and increase stack size

Display:
   Iterate through stack to show all elements
  
Top:
   Display top element on stack

Purge:
   Remove all elements on stack
   Reduce size of stack to zero
  
  


Related Solutions

in C++ For this program, you are going to implement a stack using an array and...
in C++ For this program, you are going to implement a stack using an array and dynamic memory allocation. A stack is a special type of data structure that takes in values (in our case integers) one at a time and processes them in a special order. Specifically, a stack is what's called a first-in-last-out (FILO) data structure. That is to say, the first integer inserted into the stack is the last value to be processed. The last value in...
Write a program to implement the IntStack that stores a static stack of integers and performs...
Write a program to implement the IntStack that stores a static stack of integers and performs the pop, push, isFull, and isEmpty operations. Write the main class to create a static stack of numbers 10, 20, 30, 40, and 50 then try the member functions. C++
Write a program to implement the IntStack that stores a static stack of integers and performs...
Write a program to implement the IntStack that stores a static stack of integers and performs the pop, push, isFull, and isEmpty operations. Write the main class to create a static stack of numbers 10, 20, 30, 40, and 50 then try the member functions. C++
Why do we need a dynamic stack and How to implement a dynamic array stack? (...
Why do we need a dynamic stack and How to implement a dynamic array stack? ( Please answer in Java)
Write a class VectorInt to implement the concept of one dimensional array of integers with extendable...
Write a class VectorInt to implement the concept of one dimensional array of integers with extendable array size. Your class should support storing an integer at a specific index value, retrieving the integer at a specific index value, and automatically increasing storage for the saved values.
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
USING MatLab (Arrays) Set ASIZE to 5. Write a program that creates an array of ASIZE...
USING MatLab (Arrays) Set ASIZE to 5. Write a program that creates an array of ASIZE numeric elements. Prompt the User for ASIZE numbers and store them in the array. After storing the values, calculate the sum of all the values in the array and display the sum. Modify the program you wrote for Problem 5 such that it calculates the product of all the values instead of the sum. Modify the program you wrote in Problem 6 such that...
3.1 Implement the stack ADT using array (4 marks) 3.1.1 Implement the pop() operation in the...
3.1 Implement the stack ADT using array 3.1.1 Implement the pop() operation in the stack (1 mark) Implement a stack class named Stack2540Array using array. The starter code is as follows. The instance variables and most operations are provided. You need to implement the pop operation. Make sure that your program checks whether the stack is empty in the pop operation. import java . io .*; import java . util .*; public class Stack2540Array { int CAPACITY = 128; int...
(JAVA) Why do we need a dynamic stack? How do you implement a dynamic stack array?
(JAVA) Why do we need a dynamic stack? How do you implement a dynamic stack array?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT