Question

C++ 1. Set array size to 5 2. Within your main() method, a. push the following...

C++

1. Set array size to 5

2. Within your main() method,

a. push the following values onto the stack: 2, 4, 5, 6, 8, 10, 12

b. Display what is returned from peek(), and then pop() the stack

c. repeat b. above until the stack is empty.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here I am sending code, screens of code and output. Please refer the screens. If any queries or question comment below. Thank you.

Cpp Code:

#include <iostream>
using namespace std;

int stack[5], top = -1; //declaring array size with 5.

int main()
{
int val; //declaring variable.

cout<<"Enter values to push: "; //prompting the user to enter values to push.

/*push the values in to the stack.*/
for(int i=0; i<5; i++)
{
cin>>val; //reading the values
stack[++top] = val; //push in to the stack.
}

/*displaying peek and popping the stack.*/
for(int i=4; i>=0; i--)
{
cout<<stack[top]<<"\n"; //displaying peek element.

top--; //popping the stack.
}
return 0;
}

output:

Add a comment
Know the answer?
Add Answer to:
C++ 1. Set array size to 5 2. Within your main() method, a. push the following...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT