Lesson 79: Records
Эта страница требует современного браузера (с поддержкой SharedArrayBuffer). Пожалуйста, обновите Chrome, Edge, Firefox или Safari до последней версии.
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.
Сложность 3/5 · 200 монет + небольшой бонус за первое завершение сегодня
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).
Нажмите Запустить, чтобы увидеть результат здесь.
Сложность 4/5 · 280 монет + небольшой бонус за первое завершение сегодня
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.
Нажмите Запустить, чтобы увидеть результат здесь.