Published by Patrick Mutisya · 14 days ago
Show understanding of the purpose of a record structure to hold a set of data of different data types under one identifier.
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.
record Student
name : string[30]
age : integer
gpa : real
enrolDate : date
end record
Consider a Student record that stores the following fields:
| Field | Data Type | Typical Size (bytes) |
|---|---|---|
| name | string[30] | 30 |
| age | integer | 4 |
| gpa | real (float) | 8 |
| enrolDate | date | 8 |
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}\$
Student record showing each field and any padding bytes.
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"
procedure printStudent(s : Student)
output "Name: " + s.name
output "Age: " + s.age
output "GPA: " + s.gpa
output "Enrolled: " + s.enrolDate
end procedure
printStudent(student1)