| | |
Correcting inefficiency in pseudocode
Thread Solved |
•
•
Join Date: Nov 2009
Posts: 6
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Nov 2008
Posts: 63
Reputation:
Solved Threads: 4
0
#3 25 Days Ago
•
•
•
•
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.
•
•
Join Date: Jun 2008
Posts: 14
Reputation:
Solved Threads: 1
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.
I dont see any errros in the pseudo code.
java Syntax (Toggle Plain Text)
int count = 11; int x = -1; if(count > 10) { while( x < 0) { try { x = x - 1; System.out.println("inside while "); } catch (Exception ioe) { System.out.println("IO error trying to read your name!"); } } }
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)
•
•
Join Date: Nov 2008
Posts: 63
Reputation:
Solved Threads: 4
0
#7 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.
java Syntax (Toggle Plain Text)
int count = 11; int x = -1; if(count > 10) { while( x < 0) { try { x = x - 1; System.out.println("inside while "); } catch (Exception ioe) { System.out.println("IO error trying to read your name!"); } } }
•
•
Join Date: Oct 2009
Posts: 91
Reputation:
Solved Threads: 8
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
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
![]() |
Similar Threads
- help writing pseudocode, PLEASE!!!! (C++)
- Correcting System Hang at Startup (Windows tips 'n' tweaks)
- Pseudocode (Computer Science)
Other Threads in the Java Forum
- Previous Thread: Help with Threads needed
- Next Thread: Taking too long to print out 500x500 2D Array
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal free game givemetehcodez graphics gui guidancer html ide image inetaddress integer integration intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle plazmic print problem program programming project recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows





