9618 A2 COMPUTER SCIENCE REVIEW
NOTES
Adam Li
2025 10 21
CHAPTER 13 DATA REPRESENTATION
13.1 User-defined data types
(a) Show understanding of why user-defined types are necessary
• Def It is a data type created by the programmer, not provided by
the programming language like built-in types such as INTEGER,
REAL, or STRING.
• Built-in data types (like INTEGER, REAL, STRING) cannot
store complex data in one place.
• Using separate variables or arrays to store related data is harder
to manage and can make the code confusing.
(b) Define and use non-composite types,Including enumerated, pointer
• Def: A non-composite data type holds only a single value, like
INTEGER, REAL, or BOOLEAN.
• An enumerated data type is a user-defined type that lists all
possible values it can take, usually as named constants.
•
//
TYPE <identifier> = (value1, value2, value3, ... )
//
TYPE Tmonth = (January, February, March, April,May,
June, July, August, September, October, November, December)
1
2
// Tmonth
DECLARE thisMonth : Tmonth
DECLARE nextMonth : Tmonth
//
thisMonth ← January
nextMonth ← thisMonth + 1
• Representing days of the week in a scheduling system. becuase
it limits the values to a fixed set, preventing invalid data (e.g.
”Funday” or misspelled strings).
•
TYPE Day = (Monday, Tuesday, Wednesday, Thursday,
Friday, Saturday, Sunday) // days of the week
TYPE TrafficLight = (Red, Amber, Green) // traffic light
TYPE Grade = (A, B, C, D, E, U) // grade
TYPE Season = (Spring, Summer, Autumn, Winter)
• A pointer is a data type that stores the memory address of an-
other variable.
• It is used to access and manipulate data indirectly, and is essential
for dynamic data structures like linked lists and trees.
•
//
TYPE <pointer> = ^<Typename>
// pointer
TYPE TmonthPointer = ^Tmonth
//
DECLARE monthPointer : TmonthPointer
//
monthPointer ← ^thisMonth // reference
3
myMonth ← monthPointer^ // dereference
(c) Define and use composite data types, including set, record and class/ob-
ject
• A composite data type stores multiple values in one structure;
it can be built-in (like an array) or user-defined (like a record).
• A Set is a data type that stores an unordered collection of unique
elements of the same type.
• Operations such as union, intersection, and difference are not
within the scope of the exam.
•
\\
TYPE <set-identifier> = SET OF <Basetype>
\\
DEFINE <identifier> (value1, value2, value3, ... ) :
<set-identifier>
\\
TYPE Sletter = SET OF CHAR
DEFINE vowel ('a', 'e', 'i', 'o', 'u') : Sletter
• Record data type has been covered in AS Chapter10
• Class data type will be introduced in chapter 20
(d) Choose and design an appropriate user-defined data type for a given
problem
• To store a library loan record including book title, issue date, and
return status → use a RECORD.
• To define the seasons of a year (Spring, Summer, Autumn, Win-
ter) → use an ENUMERATED type.
• To store the usernames of currently online users with no dupli-
cates → use a SET.
4
• To reference a child node in a tree structure → use a POINTER.
• To represent an exam result with score, grade, pass status, and a
method to calculate average → use a CLASS.