I am going some exercises and just want to make sure I am doing it right. It says for the following...Assume StackType is sent to int and STACK_CAPACITY is set to 5. Give the values of myTop and the contents of the array referred to by myArray in the stack s after the code segment is executed or indictate why an error occurs.

Stack s;
s.push(10);
s.push(22);
s.push(37);
s.pop();
s.pop();

I am guessing the answer is 10[6] and this is how I figured it in case I am doing it wrong. I starts with s at 5 and each push increases by one and each pop decreases by 1. Please let me know if this is not how I am supposed to do this question.

We do not know what myTop or myArray are supposed to represent.
However, it's safe to say that at the end the top (and only) element is 10, the stack size is 1 and the capacity probably will be still 5.

Edited 6 Years Ago by Aranarth: n/a

I did some additional reading and I think 5 is the total it can be so correct me if I am wrong but shouldnt it start with 0,1,2,3,4 with 0 at s, 1 at 10, etc

I figured out the last questions but I do have a question with the next one....

Here is my next question...

Stack s;
s.push(10);
s.push(9);
s.push(8);
while(!s.empty())
s.pop();

and the while (!s.empty() equation is myTop < Stack_CAPACITY - 1 so 2 < 4 so this statement can be ignored correct?

You need to express yourself more clearly. You have still not explained what myTop is supposed to be.
If you want to know what while(!s.empty())s.pop(); does: it removes all elements from the stack.

I apologize I am jus trying to understand these topics so just a little difficult to explain when I am not as familiar with the material but ill try harder

I think I have these done but not sure if they are right. As far as myTop goes I dont think I was ever given it...

1.

Stack s;

s.push(10);

s.push(22);

s.push(37);

s.pop();

s.pop();

This answer I put 10[0]

2.

Stack s;

s.push(10);

s.push(9);

s.push(8);

while(!s.empty())

s.pop();

This answer I put cannot be done because the last statement clears everything out

3.

Stack s;

for (int i = 1; i <= 6; i++)

s.push(10*i);

Cannot be done because I is multiplied by 10 and the
highest it can be is 6

4.

Stack s;

s.push(11);

i = s.top();

s.pop();

This answer I put
3[0]

This article has been dead for over six months. Start a new discussion instead.