| | |
Another pseudocode problem...
Please support our Computer Science advertiser: Learn about neural networks and artificial intelligence.
Thread Solved |
Could anyone look at this and tell me if I got it right? I had to rewrite the following code without GOTOs:
Here is my solution (I added an additional flag to get it done):
Please correct me if I'm doing something wrong.
Thanks, Waldis
Y: A
B
if a GOTO X
C
if b GOTO Y
X: DHere is my solution (I added an additional flag to get it done):
i=TRUE
while i
A
B
if not a then
i=FALSE
else
C
if b then
i=FALSE
DPlease correct me if I'm doing something wrong.
Thanks, Waldis
Okay, let's make some observations on the goto code:
First, Y is always executed at least once because the test to GOTO Y is at the end of the "block". That suggests a loop that tests the condition at the end rather than at the beginning, so we'll throw in a do..while for Y (# denotes a comment):
If you're not allowed to use a special loop like that in your pseudocode, then you have the right of it in setting a flag to a successful condition for the first iteration.
GOTO X is a classic break from the loop, so if your pseudocode can support it, that's the easiest way:
However, even though that's likely the solution that you would take in real code, BREAK is akin to GOTO in that it makes an unconditional jump, so it might not be an acceptable solution in this case. The alternative is what you did, to use flags to drive the loop:
All in all, this saves you a potentially confusing conditional branch inside the loop. But your solution is still correct.
Y: A
B
if a GOTO X
C
if b GOTO Y
X: D#Y: DO A B if a GOTO X C WHILE b X: D
GOTO X is a classic break from the loop, so if your pseudocode can support it, that's the easiest way:
#Y: DO A B if a BREAK C WHILE b D
#Y: done = false DO A B if a done = true C WHILE b AND done <> true D
New members chased away this month: 4
Thank you. If I'm correct there should be an else clause before C. Let me rewrite the pseudocoude the way I'm supposed to submit it:
I hope I'm not missing the mark by adding 'else' in there, since if done becomes TRUE it has to skip (exclude) C.
done=FALSE
repeat
A
B
if a then
done=TRUE
else
C
while b AND done NOT TRUE
DI hope I'm not missing the mark by adding 'else' in there, since if done becomes TRUE it has to skip (exclude) C.
![]() |
Similar Threads
- Hey!! Need Help With A Pseudocode Problem, Help Please (C)
- Trouble with figuring out the flowchart to pseudocode problem (Computer Science)
- PseudoCode Help For Newbie (Computer Science)
Other Threads in the Computer Science Forum
- Previous Thread: Trouble with figuring out the flowchart to pseudocode problem
- Next Thread: project management
| Thread Tools | Search this Thread |
Tag cloud for Computer Science
ai algorithm algorithms amazon assignment assignmenthelp assignments battery bigbrother binary bittorrent blogging bomb business cern clueless codebreaker compiler computer computers computerscience computertrackingsoftware conversion csc data dataanalysis dataintepretation development dfa dissertation dissertations dissertationtopic ebook employment energy extensions floatingpoint foreclosure foreclosuresoftware fuel gadgets geeks givemetehcodez government hardware homeowners homework homeworkassignment homeworkhelp ibm ideas internet iphone ipod itcontracts jobs kindle laser laws lazy linkbait mainframes marketing mobileapplication msaccess nano netbeans networking news os p2p parser piracy piratebay programming rasterizer research sam-being-cute sas science security sex simulation software spoonfeeding spying sql stephenfry student study supercomputer supercomputing syntactic technology textfield tree two'scompliment uk virus ww2






