Worked Examples
Example 1: Temperature Monitoring System
The Scenario: A greenhouse requires a program to record 24 hourly temperature readings. The program must:
- Store the readings in a 1D array.
- Calculate and output the average temperature.
- Identify and output the highest and lowest temperatures recorded.
DECLARE Temp : ARRAY[1:24] OF REAL
DECLARE Total, Average, Max, Min : REAL
DECLARE i : INTEGER
Total ← 0
// 1. Input loop with Validation
FOR i ← 1 TO 24
REPEAT
OUTPUT "Enter temperature for hour ", i
INPUT Temp[i]
UNTIL Temp[i] > -50 AND Temp[i] < 60
Total ← Total + Temp[i]
NEXT i
// 2. Initialize Max and Min with the first value in the array
Max ← Temp[1]
Min ← Temp[1]
// 3. Search Loop
FOR i ← 2 TO 24
IF Temp[i] > Max THEN Max ← Temp[i] ENDIF
IF Temp[i] < Min THEN Min ← Temp[i] ENDIF
NEXT i
Average ← Total / 24
OUTPUT "Average: ", Average
OUTPUT "Highest: ", Max, " Lowest: ", Min
DECLARE Total, Average, Max, Min : REAL
DECLARE i : INTEGER
Total ← 0
// 1. Input loop with Validation
FOR i ← 1 TO 24
REPEAT
OUTPUT "Enter temperature for hour ", i
INPUT Temp[i]
UNTIL Temp[i] > -50 AND Temp[i] < 60
Total ← Total + Temp[i]
NEXT i
// 2. Initialize Max and Min with the first value in the array
Max ← Temp[1]
Min ← Temp[1]
// 3. Search Loop
FOR i ← 2 TO 24
IF Temp[i] > Max THEN Max ← Temp[i] ENDIF
IF Temp[i] < Min THEN Min ← Temp[i] ENDIF
NEXT i
Average ← Total / 24
OUTPUT "Average: ", Average
OUTPUT "Highest: ", Max, " Lowest: ", Min
Example 2: School Grades (2D Arrays)
The Scenario: A teacher manages marks for 30 students across 3 subjects (Maths, Science, English). The program must:
- Use a 2D array to store the marks.
- Count how many students scored an average of > 80 (Distinction).
- Allow a teacher to search for a student's marks by entering their ID (1-30).
DECLARE Marks : ARRAY[1:30, 1:3] OF INTEGER
DECLARE StudentID, DistCount, Row, Col : INTEGER
DECLARE StudentSum : REAL
DistCount ← 0
// Populate the 2D Array
FOR Row ← 1 TO 30
StudentSum ← 0
FOR Col ← 1 TO 3
INPUT Marks[Row, Col]
StudentSum ← StudentSum + Marks[Row, Col]
NEXT Col
// Check for Distinction
IF (StudentSum / 3) > 80 THEN
DistCount ← DistCount + 1
ENDIF
NEXT Row
// Search Functionality
OUTPUT "Enter Student ID (1-30):"
INPUT StudentID
OUTPUT "Maths: ", Marks[StudentID, 1]
OUTPUT "Science: ", Marks[StudentID, 2]
OUTPUT "English: ", Marks[StudentID, 3]
OUTPUT "Total distinctions: ", DistCount
DECLARE StudentID, DistCount, Row, Col : INTEGER
DECLARE StudentSum : REAL
DistCount ← 0
// Populate the 2D Array
FOR Row ← 1 TO 30
StudentSum ← 0
FOR Col ← 1 TO 3
INPUT Marks[Row, Col]
StudentSum ← StudentSum + Marks[Row, Col]
NEXT Col
// Check for Distinction
IF (StudentSum / 3) > 80 THEN
DistCount ← DistCount + 1
ENDIF
NEXT Row
// Search Functionality
OUTPUT "Enter Student ID (1-30):"
INPUT StudentID
OUTPUT "Maths: ", Marks[StudentID, 1]
OUTPUT "Science: ", Marks[StudentID, 2]
OUTPUT "English: ", Marks[StudentID, 3]
OUTPUT "Total distinctions: ", DistCount
15-Mark Strategy Checklist
- Initialization: Did you set totals to 0 and max/min variables?
- Input Prompts: Did you use
OUTPUTbefore everyINPUT? - Validation: Did you use
REPEAT...UNTILfor range checks? - End Tags: Did you close every
FOR,IF, andWHILE? - Efficiency: Did you use the correct data types (
REALfor averages)?