Lesson 79: Records
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
Fields that belong together
- A record is a composite data type: one item made of several fields, which can have different data types.
- In C++ you define a record type with
struct. Then you can make variables and arrays of that type. - You reach a field with a dot:
team[1].name.
| Pseudocode (9618) | C++ |
|---|---|
| TYPE Player | struct Player { |
| DECLARE Name : STRING | String name; |
| DECLARE Score : INTEGER | int score; |
| ENDTYPE | }; |
- An array of records replaces parallel arrays (Robots lesson 48): the name and the score can never get out of step.
English
In the exam
- 9618/32/M/J/23 Q6(a): declare the composite data type TAppointments for patients of a dental clinic (name, date of birth, telephone number, last and next appointment, treatments complete yes or no). Mark scheme: TYPE TAppointments … ENDTYPE, with a DECLARE for each field, e.g. Telephone : STRING and TreatmentsComplete : BOOLEAN. (b) An array of these records: DECLARE DentalRecord : ARRAY[1:250] OF TAppointments.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
A record keeps several fields about one thing together, even of different data types. In C++ a record type is a struct. Player has only the field name, so the program does not compile. Add the field score (an int) and the field online (a bool).
Nhấn Chạy để xem kết quả ở đây.
Độ khó 4/5 · 280 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Type 4 players as name,score, for example Ana,120. Store each one in the next record of team, then, after 4, print the player with the highest score: Top: Eli 150.
Nhấn Chạy để xem kết quả ở đây.