Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
This algorithm should input five marks, each from to , and output the highest mark. It has three errors.
Highest ← 100
FOR Count ← 1 TO 4
INPUT Mark
IF Mark < Highest
THEN
Highest ← Mark
ENDIF
NEXT Count
OUTPUT "Highest mark: ", HighestNormal data is accepted. Abnormal data should be rejected. Extreme data is the largest or smallest accepted value. Boundary data is the largest or smallest accepted value and the value next to it that is rejected. Find errors by dry-running with simple data and comparing with the expected result.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Step 1. The dry run. The loop runs times, so only , , and are input. Highest starts at . The test Mark < Highest keeps the smaller value: , then , then , then . The output is .
Step 2. The errors.
FOR Count ← 1 TO 4 takes only four marks. Correction: FOR Count ← 1 TO 5.Highest ← 100 starts higher than any mark, so no mark can replace it. Correction: Highest ← 0.IF Mark < Highest finds the lowest mark. Correction: IF Mark > Highest.Step 3. The corrected algorithm.
Highest ← 0
FOR Count ← 1 TO 5
INPUT Mark
IF Mark > Highest
THEN
Highest ← Mark
ENDIF
NEXT Count
OUTPUT "Highest mark: ", HighestWith the same marks it outputs Highest mark: 90.
Answer: (a) ; (b) the loop limit, the starting value and the operator, corrected as above to give .
Watch out. With error 2 fixed but error 3 left, the output would be . Fix every error, then dry-run again.
Log in to join the discussion.
Loading…