944,209 Members | Top Members by Rank

Ad:
Jan 16th, 2006
0

Another pseudocode problem...

Expand Post »
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
Similar Threads
Reputation Points: 30
Solved Threads: 0
Newbie Poster
waldis is offline Offline
24 posts
since Jan 2006
Jan 17th, 2006
0

Re: Another pseudocode problem...

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 17th, 2006
0

Re: Another pseudocode problem...

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.
Reputation Points: 30
Solved Threads: 0
Newbie Poster
waldis is offline Offline
24 posts
since Jan 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Computer Science Forum Timeline: Trouble with figuring out the flowchart to pseudocode problem
Next Thread in Computer Science Forum Timeline: project management





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC