Dealing with errors and more work with decisions and loops - Exercise 1

Sample answer

You need to pack some books in cardboard boxes. Design a program which measures the volume of boxes by accepting as input W (width), H (height), B (breadth). Since only positive integer values are permitted assume that you have a subprocess called Input_Positive_Integer and use it. You don't have to write the pseudocode for this subprocess. The permissible range of volumes is 100 volume units to 600 volume units inclusive. Any box outside this range is rejected. Any box outside this range is rejected. The books you are packing amount to 6000 volume units in total.

Total_Volume = 0
REPEAT
 CALL Input_Positive_Integer(B)
 CALL Input_Positive_Integer(W)
 CALL Input_Positive_Integer(H)
 Volume = B * W * H
 IF (Volume >= 100) AND (Volume <= 600)
  THEN
    DISPLAY 'Box Accepted'
    Total_Volume = Total_Volume + Volume
  ELSE DISPLAY 'Box Rejected'
 ENDIF
UNTIL Total_Volume >= 6000

I'm assuming now that you have a good grasp of the strategy we are using to design algorithms so I have left that out.

You can see that I just CALL the subprocess to get integer values and assume that the subprocess is available somewhere.

 

Return to the lesson


and may not be reproduced by any means without the written
 

This publication is copyright David Beech and Learning Systems 1997-2002
and may not be reproduced by any means without the written permission of
David Beech.
9 Wyndella Street, Tasmania, Australia


db@codelearn.com