| | |
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; 24 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 |
6 @param actuate android api applet application arc array arrays automation balls binary bluetooth bold business byte c++ chat class client code codesnippet collections compare component coordinates database defaultmethod detection doctype dragging ebook eclipse educational error file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework hql html ide ideas image ingres input integer internet intersect invokingapacheantprogrammatically j2me java javaexcel javaprojects jni jpanel jtextarea julia linux list map method methods mobile mysql netbeans newbie nextline parameter php pong problem program programming project recursion recursive scanner sell server set sms sort sql string sun swing swt terminal threads tree web websites windows





