Lesson 57: Weather week
A table of readings
- A weather station takes several readings every day. A 2D array stores them like a table: one row for each day, one column for each reading.
- Two loops, one inside the other, visit every cell: the outer loop picks the day, the inner loop the reading.
- Parallel arrays use the same index for the same thing:
days[1]andreadings[1]are both Tuesday.
Words for this lesson
| Word | Meaning |
|---|---|
| 2D array | a table of values with rows and columns |
| nested loop | a loop inside another loop |
| parallel arrays | arrays where the same index means the same item |
readings[d][h]
| Day | h = 0 | h = 1 | h = 2 | h = 3 |
|---|---|---|---|---|
| Mon (d = 0) | 10.0 | 12.0 | 15.0 | 11.0 |
| Tue (d = 1) | −2.5 | 0.5 | 3.0 | 1.0 |
| Wed (d = 2) | 20.4 | 22.6 | 25.1 | 21.2 |
In the exam
- 0478/21/M/J/23 Q11 (the long programming question): a 2D array
Readings[]holds 24 hourly temperatures for each of 7 days, with the parallel arraysDays[]andAverageTemp[]. Your program must store each day's average, find the average for the whole week, convert with Fahrenheit = Celsius × 9 / 5 + 32, and output every value rounded to one decimal place with a suitable message. - That question also asks you to validate each input (−20.0 to +50.0): use the range check from lesson 40.
Difficulté 3/5 · 200 pièces + un petit bonus lors de la première réussite aujourd'hui
A weather station stores temperatures in a 2D array: readings[d][h] is reading h of day d (3 days, 4 readings a day). For each day, add up its 4 readings with a loop inside the loop, and store the average in averageTemp[d]. The program prints each day, for example Mon: 12.0 C.
Cliquez sur Exécuter pour voir le résultat ici.
Difficulté 4/5 · 280 pièces + un petit bonus lors de la première réussite aujourd'hui
Finish the function toF(c): Fahrenheit = Celsius × 9 ÷ 5 + 32. Then also add every day's total to weekTotal, and at the end print the average of all 12 readings: Week: then the value in C and in F, like the day lines. All values have one decimal place.
Cliquez sur Exécuter pour voir le résultat ici.