Worked example 1
A teacher needs a program to find the highest and lowest scores from a class of 25 students. Use stepwise refinement to design an algorithm and represent the first level of refinement.
Show solution outline
The problem is 'Find highest and lowest scores for 25 students'.
Level 0: Process class scores.
Stepwise Refinement (Level 1):
- Initialise variables (e.g., HighestScore, LowestScore).
- Input and process scores for all 25 students.
- Output the final HighestScore and LowestScore.
Further Refinement (Level 2) of Step 1 & 2: 1.1. Input the first score. 1.2. Set HighestScore and LowestScore to this first score. 2.1. Loop 24 times (for the remaining students). 2.2. Inside the loop: Input the next score. 2.3. If the new score is greater than HighestScore, update HighestScore. 2.4. If the new score is less than LowestScore, update LowestScore.
Final Pseudocode:
DECLARE HighestScore, LowestScore, StudentScore : INTEGER
INPUT StudentScore
HighestScore <- StudentScore
LowestScore <- StudentScore
FOR Counter <- 1 TO 24
INPUT StudentScore
IF StudentScore > HighestScore THEN
HighestScore <- StudentScore
ENDIF
IF StudentScore < LowestScore THEN
LowestScore <- StudentScore
ENDIF
NEXT Counter
PRINT "Highest score is: ", HighestScore
PRINT "Lowest score is: ", LowestScore