User-defined data types

Computer Science A2 · Data Representation. The idea in plain words, the rules to remember, the mistake to avoid and a worked example, written and checked by Mega Lecture.

Test this topic freeAll Computer Science A2 topics

The idea

Built-in types such as INTEGER and STRING cannot always describe the data of a real problem. A user-defined data type is designed by the programmer so that the program models the data closely, is easier to read and lets invalid values be caught.

Non-composite types do not refer to other types: an enumerated type is a list of named values in a fixed order, and a pointer holds the memory address of a value of one stated type.

Composite types are built from other types. A record groups fields of different types, a set holds values with no order and no repeats, and a class holds properties together with methods.

In simpler words

Built-in types like INTEGER and STRING do not fit every problem. So a programmer can make their own type. An enumerated type is a short list of named values, like the four seasons. A record groups several facts about one thing, like a book's title and pages. A set holds values with no order and no repeats. These types make code clearer and stop wrong values.

An everyday comparison

A record is like a form with labelled boxes: name, age, class. Every student fills in the same boxes, but with their own answers. An enumerated type is like a drop-down menu where you can only pick from the listed choices. The comparison stops working because a drop-down shows text, while enumerated values are names in the program, not strings, and they have a fixed order.

Rules to remember

Common mistake

Students write an enumerated value in quotation marks, as in Day ← "Tue". The values are identifiers, so the correct form is Day ← Tue, and a name that is not in the list is not allowed.

In the exam

Write the declaration in the syllabus pseudocode format. For an enumerated type: TYPE TSeason = (Spring, Summer, Autumn, Winter), with no quotation marks. For a record: TYPE TBook, then a DECLARE line for each field with its type, then ENDTYPE. For a pointer: TYPE TIntPtr = ^INTEGER. When asked to justify a choice, link it to the data: several facts about one thing means a record; one value from a fixed list means an enumerated type; membership with no repeats means a set.

Worked example

TIntPtr is a pointer type to INTEGER.
DECLARE A, B : INTEGER
DECLARE P : TIntPtr
A ← 10
B ← 4
P ← ^A
P^ ← P^ − 3
P ← ^B
P^ ← P^ + A
What are the final values of A and B?

  1. P ← ^A makes P point to A, so P^ ← P^ − 3 changes A: 10 − 3 = 7.
  2. P ← ^B makes P point to B; A is not changed by this.
  3. P^ ← P^ + A changes B: 4 + 7 = 11.

Answer: A = 7 and B = 11.

Ask about this

Type what you are stuck on. The helper finds the explanation, worked examples and the notes to read, free and instant.

Keep going

File organisation and access →Take a Computer Science A2 test