Lesson 79: Records
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version.
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.
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.
Difficulty 3/5 · 200 coins + a small bonus on first completion today
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).
Click Run to see the output here.
Difficulty 4/5 · 280 coins + a small bonus on first completion today
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.
Click Run to see the output here.