
May 20th, 2010, 06:33 PM
#1
stacks push\pop
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.

May 20th, 2010, 06:35 PM
#2
Re: stacks push\pop
I'm guessing that STACK_CAPACITY means the Stack uses a fixedsize array internally. Therefore, if the STACK_CAPACITY is 5, the only valid stack indexes will be 0,1,2,3 and 4.

May 20th, 2010, 06:40 PM
#3
Re: stacks push\pop
so 0 would be s, 1 would be 10, etc, etc?

May 20th, 2010, 06:43 PM
#4
Re: stacks push\pop
I'm not sure where you're getting the idea that "s" is at one of the indexes....s is the stack object as a whole.

May 20th, 2010, 06:47 PM
#5
Re: stacks push\pop
ok so 0 is 10, 1 is 22, and 2 is 37....so with 2 .pop then the answer would be 10[0]

May 20th, 2010, 06:48 PM
#6
Re: stacks push\pop
That would be my interpretation, yes.

May 20th, 2010, 06:59 PM
#7
Re: stacks push\pop
OK thanks and by the way u dont have to help me through all of these especially if you are busy...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?

May 20th, 2010, 07:18 PM
#8
Re: stacks push\pop
did some additonal reading and shouldnt this remove everything from the stack...While (!s.empty()) = myTop

May 20th, 2010, 07:34 PM
#9
Re: stacks push\pop
Originally Posted by jimJohnson123
and the while (!s.empty() equation is myTop < Stack_CAPACITY  1 so 2 < 4 so this statement can be ignored correct?
Curious. That would seem to imply the myTop index increments downwards from STACK_CAPACITY to 0 rather than the other way around, which is a bit odd, but a valid design.

May 20th, 2010, 07:40 PM
#10
Re: stacks push\pop
Aight this is what I got for all four problems so maybe I should turn it around. I am not asking for answers but for these 4 problems if any feedback could be offered Id really appreciate 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 it goes empty and then removes a s.pop
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]
If these are wrong can someone explain to me what I need to put in here

May 21st, 2010, 05:40 PM
#11
Re: stacks push\pop
2.
it only pops when s is NOT empty
3.
why does multiply by ten matter when that is the value put into the stack? What you should look at is how many times push() is called in the for loop.
4. where does your answer '3' come from?
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!
