Show understanding of the purpose of a record structure to hold a set of data of different data types under one identifier

Published by Patrick Mutisya · 14 days ago

Cambridge A-Level Computer Science 9618 – 10.1 Data Types and Records

10.1 Data Types and Records

Objective

Show understanding of the purpose of a record structure to hold a set of data of different data types under one identifier.

What is a Record?

A record (also called a structure in some languages) is a composite data type that groups together a fixed number of elements, called fields, each of which may have a different data type. All the fields are accessed through a single identifier.

Why Use Records?

  • Logical grouping of related data (e.g., all information about a student).
  • Simplifies passing multiple values to functions or procedures – only one argument is needed.
  • Improves code readability and maintainability.
  • Provides a clear mapping between real‑world entities and program data.

Typical Syntax (Pseudocode)

record Student

name : string[30]

age : integer

gpa : real

enrolDate : date

end record

Example Record: Student

Consider a Student record that stores the following fields:

FieldData TypeTypical Size (bytes)
namestring[30]30
ageinteger4
gpareal (float)8
enrolDatedate8

Memory Layout and Size Calculation

The total size of a record is the sum of the sizes of its fields, plus any padding required for alignment. Assuming no padding, the size is:

\$\text{Size}_{\text{Student}} = 30 + 4 + 8 + 8 = 50\ \text{bytes}\$

If the target architecture aligns fields on 4‑byte boundaries, padding may be added after the name field (30 bytes) to reach the next multiple of 4, giving an extra 2 bytes of padding:

\$\text{Size}_{\text{Student}} = (30 + 2) + 4 + 8 + 8 = 52\ \text{bytes}\$

Suggested diagram: Memory layout of a Student record showing each field and any padding bytes.

Accessing Record Fields

Fields are accessed using the dot operator (.) after the record variable name.

student1 : Student

student1.name = "Alice Johnson"

student1.age = 20

student1.gpa = 3.75

student1.enrolDate = "2023‑09‑01"

Passing Records to Functions

  1. Define a function that takes a record as a parameter.
  2. Call the function with a record variable; the whole record is passed as a single argument.

procedure printStudent(s : Student)

output "Name: " + s.name

output "Age: " + s.age

output "GPA: " + s.gpa

output "Enrolled: " + s.enrolDate

end procedure

printStudent(student1)

Comparison with Arrays

  • Arrays hold multiple values of the same data type, indexed by an integer.
  • Records hold a fixed set of values of different data types, each identified by a field name.
  • Use an array when you need a homogeneous collection; use a record when you need to model a single entity with heterogeneous attributes.

Key Take‑aways

  • A record groups related data of different types under one identifier.
  • It simplifies data handling, improves program structure, and mirrors real‑world objects.
  • Understanding memory layout helps predict storage requirements and performance implications.