ROUND, RANDOM and a trace
Trang này cần trình duyệt gần đây (hỗ trợ SharedArrayBuffer). Vui lòng cập nhật Chrome, Edge, Firefox hoặc Safari lên phiên bản mới nhất.
English
A shop rounds a price
ROUND(Value, Places)returnsValuerounded toPlacesdecimal places.Placesis an integer.ROUND(3.14159, 2)is3.14.ROUND(9.6, 0)is the whole number10.- The syllabus lists
ROUNDwithDIV,MODandRANDOM.
English
DECLARE Cost : REAL
OUTPUT ROUND(7.456, 1)
Cost ← 18 / 7
OUTPUT ROUND(Cost, 2)
OUTPUT ROUND(Cost, 0)
18 / 7is2.5714…. Rounded to 2 places it is2.57; to 0 places it is3.
English
A dice uses RANDOM
RANDOM()returns a real from 0 to 1, including both ends. Each run can differ.ROUND(RANDOM() * 6, 0)gives a whole number from 0 to 6, so it is not a dice: 0 and 6 also come up only half as often as the others.1 + ROUND(RANDOM() * 5, 0)gives 1 to 6, though 1 and 6 are still less likely. The paper usesRANDOMwhen the result may vary, so you are not asked for one fixed output.
English
OUTPUT ROUND(RANDOM() * 6, 0)
English
Fill a trace table
- A trace table is a dry run. One row is one pass of the loop. You write the variables after that pass.
Valuestarts at 1 and doubles on every pass. AfterI = 1it is 2, afterI = 2it is 4, and so on.
| I | Value |
|---|---|
| 1 | 2 |
| 2 | 4 |
| 3 | 8 |
| 4 | 16 |
Khám phá
One row per pass
A trace table records each variable after that pass of the loop. Here Total is the running sum of I.
English
DECLARE I, Value : INTEGER
Value ← 1
FOR I ← 1 TO 4
Value ← Value * 2
OUTPUT I, " ", Value
NEXT I
- The
OUTPUTinside the loop prints one row of the table per pass, so the program checks your dry run for you.
English
Watch out
- Round a half only when you have checked the digit. The tasks here are not on a halfway value.
RANDOM()is notRAND.RANDbelongs to the 9618 guide, not to this syllabus.
English
Common mistakes
ROUNDtakes the value first and the number of places second.- In a trace, record the values after the line has run, not the values you hope the next line will produce.
English
Now you try
- The first task is
ROUND. The second prints a trace, one row per line. - Press Check answer to test it.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Đầu ra ROUND(3.14159, 2) và ROUND(9.6, 0), each on its own line.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Bắt đầu Total at 0. For I from 1 đến 3, add I đến Total and output I, a space, and Total on that line. The three lines are the trace.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Read a REAL Price. Add 20% tax and output the result rounded to 2 decimal places with ROUND.
Nhấn Chạy để xem kết quả ở đây.