Another pseudocode problem...

Please support our Computer Science advertiser: Learn about neural networks and artificial intelligence.
Thread Solved

Join Date: Jan 2006
Posts: 24
Reputation: waldis is an unknown quantity at this point 
Solved Threads: 0
waldis's Avatar
waldis waldis is offline Offline
Newbie Poster

Another pseudocode problem...

 
0
  #1
Jan 16th, 2006
Could anyone look at this and tell me if I got it right? I had to rewrite the following code without GOTOs:

Y:  A
     B
     if a GOTO X
     C
     if b GOTO Y
X:  D

Here 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
D

Please correct me if I'm doing something wrong.

Thanks, Waldis
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,848
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Another pseudocode problem...

 
0
  #2
Jan 17th, 2006
Okay, let's make some observations on the goto code:
Y:  A
     B
     if a GOTO X
     C
     if b GOTO Y
X:  D
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):
#Y:
DO
  A
  B
  if a GOTO X
  C
WHILE b
X: D
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:
#Y:
DO
  A
  B
  if a BREAK
  C
WHILE b
D
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:
#Y:
done = false
DO
  A
  B
  if a done = true
  C
WHILE b AND done <> true
D
All in all, this saves you a potentially confusing conditional branch inside the loop. But your solution is still correct.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 24
Reputation: waldis is an unknown quantity at this point 
Solved Threads: 0
waldis's Avatar
waldis waldis is offline Offline
Newbie Poster

Re: Another pseudocode problem...

 
0
  #3
Jan 17th, 2006
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:

done=FALSE
repeat
  A
  B
  if a then
    done=TRUE
  else
    C
  while b AND done NOT TRUE
D

I hope I'm not missing the mark by adding 'else' in there, since if done becomes TRUE it has to skip (exclude) C.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



Tag cloud for Computer Science
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC