統合
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 コイン + 今日初回達成時の小ボーナス
配列は既に入力されています。1行目にTotal = 25を出力し、25が20以上であるためtarget metを出力します。
実行ボタンをクリックして出力を確認してください。
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
5つの整数値をMark : ARRAY[1:5] OF INTEGERに読み込みます。平均値より厳密に大きい値の個数を出力しなさい。
実行ボタンをクリックして出力を確認してください。
難易度 3/5 · 200 コイン + 今日初回達成時の小ボーナス
図書館の座席数 Capacity を読み取ります。次に NumberOfGroups (1 から 5) を読み取り、NumberOfGroups requested group sizes(グループ人数)を読み取ります。残りの座席に収まる場合のみ正のグループを受理します。各グループについて yes/no を出力し、使用済みの座席数を出力します。
実行ボタンをクリックして出力を確認してください。