Putting it together
One program uses several constructs
- A 9618 answer mixes
DECLARE,←,IF…THEN,FORandOUTPUT. - Indent the body of a loop. Put
THENon theIFline. - A short comment with
//says what a block is for.
// Add up a week of rainfall, then say whether it was a wet week.
DECLARE Rain : ARRAY[1:7] OF INTEGER
DECLARE Total : INTEGER
DECLARE Day : INTEGER
Rain[1] ← 0
Rain[2] ← 12
Rain[3] ← 3
Rain[4] ← 0
Rain[5] ← 8
Rain[6] ← 15
Rain[7] ← 1
Total ← 0
FOR Day ← 1 TO 7
Total ← Total + Rain[Day]
NEXT Day
OUTPUT "Rain this week = ", Total, " mm"
IF Total >= 30 THEN
OUTPUT "a wet week"
ELSE
OUTPUT "a dry week"
ENDIF
- Three constructs in order: a sequence sets up the data, a loop builds the total, a selection chooses the message.
Worked example: plan, then two passes
Question: on which days was it warmer than the week's average, and how many were there?
- Input: seven temperatures, stored in an array.
- Process: you cannot compare a day with the average until you know the average. So loop once to find the total, then loop again to compare.
- Output: the average, each warmer day, then the count.
DECLARE Temp : ARRAY[1:7] OF INTEGER
DECLARE Total, Warmer, Day : INTEGER
DECLARE Average : REAL
Temp[1] ← 18
Temp[2] ← 21
Temp[3] ← 17
Temp[4] ← 24
Temp[5] ← 19
Temp[6] ← 16
Temp[7] ← 25
// Pass 1: the average needs every value first
Total ← 0
FOR Day ← 1 TO 7
Total ← Total + Temp[Day]
NEXT Day
Average ← Total / 7
OUTPUT "Average = ", Average
// Pass 2: compare each day with the average
Warmer ← 0
FOR Day ← 1 TO 7
IF Temp[Day] > Average THEN
OUTPUT "Day ", Day, ": ", Temp[Day]
Warmer ← Warmer + 1
ENDIF
NEXT Day
OUTPUT Warmer, " days were warmer than average"
The total is 140, so the average is 20. Days 2, 4 and 7 (21, 24 and 25) are above it. Day 5 (19) is below it, so it is not counted.
| Pass | What the loop knows | What it does |
|---|---|---|
| 1 | nothing yet | adds all seven values: Total = 140 |
| between | Total |
Average ← 140 / 7, which is 20 |
| 2 | Average |
compares each day with 20 and counts the warmer ones |
Common mistakes
- Setting
Total ← 0inside the loop. It is reset on every pass, so only the last value is kept. - Comparing in the same loop that builds the total. The average is not known yet.
- Using
Iwithout aDECLARE. A 9618 answer declares every variable, loop counters too. - Writing
>=when the question says "above". Check what happens to a value that is exactly equal.
What this course left for A2
- Random files, classes, enumerated types and pointers are in the full 9618 guide.
- They are assessed at A2, not in this AS course.
- The IGCSE course on this site uses the 0478 rules for 2026–2028. Do not copy those into a 9618 answer.
Now you try
- The tasks use five marks instead of rainfall and temperatures. Plan the passes first: what must be known before you can compare?
Independent transfer
The final task has no supplied algorithm. Plan its inputs, processing and outputs, then solve it before opening support. Check more than the sample case.
Сложность 1/5 · 100 монет + небольшой бонус за первое завершение сегодня
The array is filled for you. Output Total = 25 on one line, then target met because 25 is at least 20.
Нажмите Запустить, чтобы увидеть результат здесь.
Сложность 2/5 · 140 монет + небольшой бонус за первое завершение сегодня
Read five integer marks into Mark : ARRAY[1:5] OF INTEGER. Output how many marks are strictly above their average.
Нажмите Запустить, чтобы увидеть результат здесь.
Сложность 3/5 · 200 монет + небольшой бонус за первое завершение сегодня
A library has Capacity seats. Read Capacity, then NumberOfGroups (1 to 5), then NumberOfGroups requested group sizes. Accept a positive group only if it fits in the remaining seats. Output yes/no per group, then occupied seats.
Нажмите Запустить, чтобы увидеть результат здесь.