944,085 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 618
  • Java RSS
Nov 3rd, 2009
0

Correcting inefficiency in pseudocode

Expand Post »
I have been asked to find the inefficiency in a piece of pseducode. I've looked at it but can't see it.

IF count > 10 THEN
WHILE x < 0 DO
INPUT x
ENDWHILE
ENDIF

My understanding:
Count is evaluated. If count is <10, the app moves onto the next section. If count is > 10, X is evaluated. Until x is <0, the user will keep being prompted to input x. Once they input x>=0, the loop finishes.

Please note: this is one question on a mock test - it is the whole code. I'm supposed to find the problem with this snippet. eg. there is no mention of x being a value outside of the IF section.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tom.t is offline Offline
6 posts
since Nov 2009
Nov 3rd, 2009
0
Re: Correcting inefficiency in pseudocode
. check x on input, where x is old
. if x >=0, INPUT never occurs, then x remain forever old >= 0 value
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Nov 3rd, 2009
0
Re: Correcting inefficiency in pseudocode
Click to Expand / Collapse  Quote originally posted by tom.t ...
IF count > 10 THEN
WHILE x < 0 DO
INPUT x
ENDWHILE
ENDIF

My understanding:
Count is evaluated. If count is <10, the app moves onto the next section. If count is > 10, X is evaluated. Until x is <0, the user will keep being prompted to input x. Once they input x>=0, the loop finishes.
Also, what I'm seeing is that if count has an inital value less than 10, it looks like it will stay that way. Look at the code again and ask, "How will my final condition (count being greater than or equal to 10) be satisfied? Because I don't see it in this sample. (hint: where is count incremented?)
Reputation Points: 31
Solved Threads: 4
Junior Poster in Training
eggmatters is offline Offline
67 posts
since Nov 2008
Nov 3rd, 2009
0
Re: Correcting inefficiency in pseudocode
Thank you both for your suggestions. I think I was a bit thrown by the abstract nature of the question but what you both say makes sense.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tom.t is offline Offline
6 posts
since Nov 2009
Nov 4th, 2009
0

Correcting inefficiency in pseudocode

I tried the following code which is inline with the pseudo code in the original code which works fine.
I dont see any errros in the pseudo code.

java Syntax (Toggle Plain Text)
  1. int count = 11;
  2. int x = -1;
  3. if(count > 10) {
  4. while( x < 0) {
  5. try {
  6. x = x - 1;
  7. System.out.println("inside while ");
  8. } catch (Exception ioe) {
  9. System.out.println("IO error trying to read your name!");
  10.  
  11. }
  12. }
  13. }
Last edited by peter_budo; Nov 4th, 2009 at 5:25 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reputation Points: 7
Solved Threads: 1
Newbie Poster
shobhit123 is offline Offline
14 posts
since Jun 2008
Nov 4th, 2009
0
Re: Correcting inefficiency in pseudocode
Thanks for that.

I think the question is trying to get us to think about efficiency rather than correcting an error. At least, that's my guess based on the wording.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tom.t is offline Offline
6 posts
since Nov 2009
Nov 4th, 2009
0
Re: Correcting inefficiency in pseudocode
Click to Expand / Collapse  Quote originally posted by shobhit123 ...
I tried the following code which is inline with the pseudo code in the original code which works fine.
I dont see any errros in the pseudo code.

java Syntax (Toggle Plain Text)
  1. int count = 11;
  2. int x = -1;
  3. if(count > 10) {
  4. while( x < 0) {
  5. try {
  6. x = x - 1;
  7. System.out.println("inside while ");
  8. } catch (Exception ioe) {
  9. System.out.println("IO error trying to read your name!");
  10.  
  11. }
  12. }
  13. }
The code wasn't in "error" so to speak. He was asked to find the inefficiencies with it. When you ran your program, how did you end it? Did the program exit gracefully?
Reputation Points: 31
Solved Threads: 4
Junior Poster in Training
eggmatters is offline Offline
67 posts
since Nov 2008
Nov 4th, 2009
0
Re: Correcting inefficiency in pseudocode
If you were to assume the "count" is incremented in another loop, you could have the test for the value of "count" exit sooner, which could potentially be more efficient depending on the size of the test for "x"

IF count > 10 THEN
WHILE x < 0 DO
INPUT x
ENDWHILE
ENDIF

...becomes...

IF count > 10 THEN
CONTINUE // or return
ENDIF

WHILE x < 0 DO
INPUT x
ENDWHILE
Featured Poster
Reputation Points: 304
Solved Threads: 277
Posting Virtuoso
thines01 is offline Offline
1,694 posts
since Oct 2009
Nov 4th, 2009
0
Re: Correcting inefficiency in pseudocode
Another good suggestion. Everyone has given me a lot to think about, and a good chance of answering the question (which will of course be different) on the exam.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tom.t is offline Offline
6 posts
since Nov 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Help with Threads needed
Next Thread in Java Forum Timeline: Taking too long to print out 500x500 2D Array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC