Lesson 79: Records
Esta página necesita un navegador reciente (con soporte para SharedArrayBuffer). Por favor, actualiza Chrome, Edge, Firefox o Safari a la última versión.
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.
Dificultad 3/5 · 200 monedas + un pequeño bono en el primer intento de hoy
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).
Haz clic en Ejecutar para ver la salida aquí.
Dificultad 4/5 · 280 monedas + un pequeño bono en el primer intento de hoy
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.
Haz clic en Ejecutar para ver la salida aquí.