Correcting inefficiency in pseudocode

Thread Solved

Join Date: Nov 2009
Posts: 6
Reputation: tom.t is an unknown quantity at this point 
Solved Threads: 0
tom.t tom.t is offline Offline
Newbie Poster

Correcting inefficiency in pseudocode

 
0
  #1
26 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz
 
0
  #2
26 Days Ago
. check x on input, where x is old
. if x >=0, INPUT never occurs, then x remain forever old >= 0 value
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 63
Reputation: eggmatters is an unknown quantity at this point 
Solved Threads: 4
eggmatters eggmatters is offline Offline
Junior Poster in Training
 
0
  #3
25 Days Ago
Originally Posted by tom.t View Post
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?)
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 6
Reputation: tom.t is an unknown quantity at this point 
Solved Threads: 0
tom.t tom.t is offline Offline
Newbie Poster
 
0
  #4
25 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 14
Reputation: shobhit123 is an unknown quantity at this point 
Solved Threads: 1
shobhit123 shobhit123 is offline Offline
Newbie Poster

Correcting inefficiency in pseudocode

 
0
  #5
25 Days Ago
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.

  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; 25 Days Ago at 5:25 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 6
Reputation: tom.t is an unknown quantity at this point 
Solved Threads: 0
tom.t tom.t is offline Offline
Newbie Poster
 
0
  #6
25 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 63
Reputation: eggmatters is an unknown quantity at this point 
Solved Threads: 4
eggmatters eggmatters is offline Offline
Junior Poster in Training
 
0
  #7
25 Days Ago
Originally Posted by shobhit123 View Post
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.

  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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 91
Reputation: thines01 is an unknown quantity at this point 
Solved Threads: 8
thines01 thines01 is offline Offline
Junior Poster in Training
 
0
  #8
24 Days Ago
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 6
Reputation: tom.t is an unknown quantity at this point 
Solved Threads: 0
tom.t tom.t is offline Offline
Newbie Poster
 
0
  #9
24 Days Ago
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC