Jump to content

Can you please let me know if this right?

- - - - -

  • Please log in to reply
9 replies to this topic

#1
lina

lina

    Learning Programmer

  • Members
  • PipPipPip
  • 34 posts
1-Suppose an initially empty stack S has performed a total of 25 push operations, 12 top operations, and 10 pop operations, 3 of which generated EmptyStackExceptions, which were caught and ignored. What is the current size of S?


This what I did:
A total of 25 push operations performed on empty stack S indicate That S has 25 elements initially. Then 10 top operations were performed on S but it won’t affect the elements. Lastly, 10 pop operation was performed on S but 3 of them generated a EmptyStackException, which means that the size of S is 25 – 10 + 3 or 18 elements.

2- If we implemented the stack S from Problem 1 with an array, as described in chapter 5, then what is the current value of the top instance variable?

This what I did:

The current top instance variable is one less than the size of S, which is 17.

#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
How can a stack throw an EmptyStackException if the stack isn't empty?

*Edit, unless those operations were not done in that order. Duh on my part.
I'd say anywhere from 15-18 elements.

Edited by lethalwire, 04 March 2011 - 11:25 AM.


#3
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Based on the wording of the original problem, it seems to suggest the stack underwent those operations, but not necessarily in that order. That's the only way there could be empty stack exceptions. Sounds to me like the problem is asking, "The stack underwent these operations in total, while experiencing these errors."

If there were a total of 10 pop operations, but 3 of them were unsuccessful, you can infer that 7 of the pop operations were successful.

As for part 2, there's no way of knowing without further information. Either the problem is worded very poorly, or there are some parts missing.

#4
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

gregwarner said:

Based on the wording of the original problem, it seems to suggest the stack underwent those operations, but not necessarily in that order. That's the only way there could be empty stack exceptions. Sounds to me like the problem is asking, "The stack underwent these operations in total, while experiencing these errors."

If there were a total of 10 pop operations, but 3 of them were unsuccessful, you can infer that 7 of the pop operations were successful.

As for part 2, there's no way of knowing without further information. Either the problem is worded very poorly, or there are some parts missing.

Missing parts, yes.

Inferring that 7 pop operations were successful, no.
The top() operation can also throw EmptyStackExceptions.

#5
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas

lethalwire said:

Missing parts, yes.

Inferring that 7 pop operations were successful, no.
The top() operation can also throw EmptyStackExceptions.

I inferred from the wording "and 10 pop operations, 3 of which generated EmptyStackExceptions" that it meant 3 of the pop operations generated exceptions, but you could also parse it your way too. Just another testament to how poorly the original question was worded. The original poster should find out where the question came from and ask for clarification.

#6
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

gregwarner said:

I inferred from the wording "and 10 pop operations, 3 of which generated EmptyStackExceptions" that it meant 3 of the pop operations generated exceptions, but you could also parse it your way too. Just another testament to how poorly the original question was worded. The original poster should find out where the question came from and ask for clarification.

You're right.
I googled the question and it comes from an older data structures book. There needs to be a set of standard rules or something similar to regulate how people post their questions.

#7
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts

lethalwire said:

There needs to be a set of standard rules or something similar to regulate how people post their questions.

We should write an English parser that parses an English sentence for ambiguities, and if it finds an ambiguous statement (which in English it will frequently), it should disallow the post. :)

That or we could always write in Lojban.
Wow I changed my sig!

#8
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas

ZekeDragon said:

That or we could always write in Lojban.

Oh good lord, please no.

:P

#9
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

ZekeDragon said:

We should write an English parser that parses an English sentence for ambiguities, and if it finds an ambiguous statement (which in English it will frequently), it should disallow the post. :)

That or we could always write in Lojban.

I do my crossword puzzles in Lojban :P

#10
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
@OP: In the way that I interpret that sentence, yes, what you have is generally accurate. You should have 18 elements in the list, and assuming the stack is implemented with an array, the array index for that 18th element should be 17.
Wow I changed my sig!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users