| | |
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 24 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; 23 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 23 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 23 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 |
-xlint actionlistener android api applet application array arrays automation bi binary blackberry block bluetooth character chat class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp game gameprogramming givemetehcodez graphics gui html ide image integer j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux list loops mac map method methods mobile netbeans newbie notdisplaying number online printf problem program programming project properties qt recursion researchinmotion rotatetext rsa scanner screen server set singleton sms sort sql string swing system textfields threads time title tree tutorial-sample update variablebinding windows working xor





