AQA GCSE Computer Science (8525), section 3.2.6 (Data structures)
What a record is
A record stores related data about one thing under a single name. Its parts are called fields, and each field can have a different data type. (An array holds items that are all the same type.)
Defining a record
RECORD Car
make : String
model : String
reg : String
price : Real
noOfDoors : Integer
ENDRECORD
Using a record
Fields are accessed with a dot:
myCar ← Car("Ford", "Fiesta", "AB12 CDE", 8500.00, 5)
OUTPUT myCar.make
myCar.price ← 8200.00
Python
Python has no RECORD, but a dictionary or a class can do the same job.
my_car = {
"make": "Ford",
"model": "Fiesta",
"reg": "AB12 CDE",
"price": 8500.00,
"doors": 5,
}
print(my_car["make"]) # Ford
my_car["price"] = 8200.00
print(my_car["price"]) # 8200.0
An array of records
A list of records is useful for a table of data, such as a car showroom.
showroom = [
{"make": "Ford", "price": 8500.00},
{"make": "Kia", "price": 9900.00},
]
for car in showroom:
print(car["make"], car["price"])
Record or array?
| Array | Record | |
|---|---|---|
| Items | all the same data type | fields can be different types |
| Items found by | index (number) | field name |
| Used for | a list of similar values | one thing with several details |
Common mistake: saying a record can only store one type of data. The point of a record is that its fields can have different data types. Also, do not confuse a field (one piece of data) with a record (the whole group).
Exam tip: when asked to define a record, give the record name, then each field name with its data type (choose sensible types), then ENDRECORD.
Check yourself
What is the difference between a record and an array?
A record can store fields of different data types, found by name, whereas an array stores items of the same type, found by index.
Write the definition of a record Pupil with a name and an age.
RECORD Pupil, name : String, age : Integer, ENDRECORD.
How do you refer to the field age of a record called pupil1?
pupil1.age.