Night 20: Weekly report
Anda sudah menyelesaikan malam ini dalam Python atau pseudocode. Hasilnya dihitung untuk keduanya. Berlatihlah lagi jika Anda mau.
Tonight: weekly report
- Your pet counted the stars every night this week. Tonight you write a report.
- You will learn one thing: three classic loops: total, largest and smallest.
Words for tonight
| Word | Meaning |
|---|---|
| total | all the numbers added together |
| largest / smallest | the biggest / the least |
| average | the total divided by how many numbers there are |
The total
- A running total, like night 7. Start at 0 and add each element.
DECLARE Stars : ARRAY[1:4] OF INTEGER
DECLARE Index, Total : INTEGER
Stars[1] ← 3
Stars[2] ← 9
Stars[3] ← 2
Stars[4] ← 8
Total ← 0
FOR Index ← 1 TO 4
Total ← Total + Stars[Index]
NEXT Index
OUTPUT "Total: ", Total
OUTPUT "Average: ", Total / 4

The largest
- Start with the first element. Look at the rest. Keep any bigger one.
DECLARE Stars : ARRAY[1:4] OF INTEGER
DECLARE Index, Largest : INTEGER
Stars[1] ← 3
Stars[2] ← 9
Stars[3] ← 2
Stars[4] ← 8
Largest ← Stars[1]
FOR Index ← 2 TO 4
IF Stars[Index] > Largest THEN
Largest ← Stars[Index]
ENDIF
NEXT Index
OUTPUT "Most stars: ", Largest
- For the smallest, use
<instead of>.
Three patterns for the exam
| You want | Start with | In the loop |
|---|---|---|
| total | Total ← 0 |
Total ← Total + Stars[Index] |
| largest | Largest ← Stars[1] |
keep it if Stars[Index] > Largest |
| smallest | Smallest ← Stars[1] |
keep it if Stars[Index] < Smallest |
- These three loops come up in Paper 2 again and again. Now write your pet's report!
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
Your pet counted stars for 7 nights. Use a FOR loop to add them up in Total. Output Total: 35.
Klik Jalankan untuk melihat output di sini.
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
Find the most stars on one night in Largest, and the fewest in Smallest. Output them on two lines: 9, then 1.
Klik Jalankan untuk melihat output di sini.
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
Work out the average: the total divided by 7. Output it: 5.0.
Klik Jalankan untuk melihat output di sini.
Kesulitan 3/5 · 200 koin + bonus kecil pada penyelesaian pertama hari ini
Which night was best? Keep the indeks of the biggest number in Best. Output Best night: 2.
Klik Jalankan untuk melihat output di sini.