CSwithZain
WhatsApp Join a class

A Level 9618 · Paper 3 · 13.1

User-defined data types

Why programmers build their own data types, and how to define them in pseudocode. Read the notes, try the past-paper questions, then check the mark scheme.

Past papers from June 2021 to June 202623 questions · 141 marks

    In this subtopic

    • Why user-defined data types are needed
    • Non-composite types: enumerated and pointer
    • Composite types: set, record and class (object)
    • Choosing and designing the right type for a given problem

    13.1 · 1 of 9Book page Contents

    Built-in data types

    Built-in data types are the data types that a programming language already has. They can be used straight away in a program, without the programmer having to create them. Every programming language has its own set of built-in data types.

    In one line

    • A built-in data type can be used directly by the programmer to declare and store variables in a program.
    Data typeDescriptionPseudocodePython
    booleanLogical values, True (1) and False (0)BOOLEANbool
    charA single characterCHARnone (a string of length 1)
    dateA value that represents a dateDATEdatetime class
    integerA whole number, positive or negativeINTEGERint
    realA number with a decimal pointREALfloat
    stringA sequence of charactersSTRINGstr

    13.1 · 2 of 9Book pages Contents

    User-defined data types

    A user-defined data type is a data type that the programmer designs for use within a program, as opposed to a built-in data type. It is built from other data types, which can be built-in or user-defined themselves.

    What is a user-defined data type

    • A data type constructed by a programmer, not a primitive (built-in) data type.
    • A data type that references at least one other data type (the data types can be primitive or user-defined).

    Purpose of user-defined data types

    • To create a new data type from existing data types.
    • To allow data types that are not available in a programming language to be constructed, or to extend the flexibility of the programming language.
    • The programmer can specify a new data type that meets the exact requirements of the application.

    13.1 · 3 of 9Book pages Contents

    Non-composite and composite types

    User-defined data types come in two kinds:

    • A non-composite data type is defined without reference to another data type. Enumerated and pointer types are non-composite.
    • A composite data type is built from other data types: it holds several values, of the same or different types, under one declaration. Record data types, set data types and classes are composite.
    Examples
    Non-composite (user-defined)Enumerated, pointer
    Other non-composite typesInteger, real, Boolean, char, string
    Composite (user-defined)Record, set, class
    Other composite typesArray, list, linked list, dictionary

    13.1 · 4 of 9Book pages Contents

    Enumerated data types

    An enumerated data type is a non-composite data type defined from an ordered list of values. Variables can be declared with this data type and assigned one of the values in the list.

    This pseudocode declares an enumerated data type for months:

    TYPE TMonth = (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)
    // The TMonth data type is declared
    
    DECLARE ThisMonth : TMonth
    // A variable is declared with the TMonth enumerated data type
    
    ThisMonth ← May
    // The variable ThisMonth is given the value May
    
    ThisMonth > Feb
    // This is TRUE, as May is later in the list than Feb

    Note The values in an enumerated data type are not strings, so do not put quotation marks around them.

    Exam definition

    • A user-defined non-composite data type with an ordered list of possible values.

    Practise this: past-paper questions

    Show 19 more

    13.1 · 5 of 9Book pages Contents

    Pointer data types

    A pointer data type is used to reference a memory location. It has to state the type of data that it points to.

    This pseudocode declares a pointer data type and a pointer, and then uses the pointer:

    TYPE TMyPointer = ^INTEGER
    // TMyPointer is a pointer data type that points to integers
    
    DECLARE IntegerPointer : TMyPointer
    // A variable of the TMyPointer data type
    
    DECLARE MyNum, YourNum : INTEGER
    MyNum ← 23
    // Two integer variables; one of them is given a value
    
    IntegerPointer ← @MyNum
    // IntegerPointer now holds the address of MyNum
    
    YourNum ← IntegerPointer^
    // Reads the value stored at the address IntegerPointer points to.
    // This is called dereferencing.

    Pointers are used to build more complex data structures, such as linked lists and binary trees.

    Exam definition

    Used later in

    13.1Choosing a data typepointer data type19.1Algorithmsbinary tree, linked list

    13.1 · 6 of 9Book page Contents

    Record data types

    A record data type stores a collection of related data about one thing, like a record in a database. It is made of several fields, and each field has its own data type, so a record is a composite data type.

    This pseudocode defines a record type for a student and then uses it:

    TYPE TStudentRecord
       DECLARE FirstName : STRING
       DECLARE LastName  : STRING
       DECLARE Absences  : INTEGER
       DECLARE Class     : STRING
    ENDTYPE
    
    DECLARE Student1 : TStudentRecord
    // The variable is declared as a record
    
    Student1.FirstName ← "Zain"
    Student1.LastName  ← "Merchant"
    Student1.Absences  ← 0
    Student1.Class     ← "A2 Level"
    // Each field is reached using dot notation

    Practise this: past-paper questions

    Show 27 more

    Used later in

    13.1Choosing a data typerecord data type13.2Files, records and fieldsrecord data type20.2File Processing and Exception Handlingrandom-access file handling

    13.1 · 7 of 9Book page Contents

    Set data types

    A set data type is a composite data type that lets a programmer use set operations on data. The values in a set have no order and no repeats. The operations usually include:

    • union
    • difference
    • intersection
    • including an element
    • excluding an element
    • checking whether an element is in a set
    TYPE LetterSet = SET OF CHAR
    // LetterSet is a set data type that holds characters
    
    DEFINE Vowels ('A', 'E', 'I', 'O', 'U') : LetterSet
    // Vowels is a set of type LetterSet with these values

    13.1 · 8 of 9Book page Contents

    Classes and objects

    An object (or class) data type is a composite data type used in object-oriented programming. A class describes the data and the procedures that act on that data; an object is one instance of a class. In a way, objects are records with their own functions attached.

    Used later in

    20.1Programming Paradigmsobject-oriented programming

    13.1 · 9 of 9Book page Contents

    Choosing a data type

    An exam question may describe a situation and ask you to choose a suitable data type, or to write the pseudocode that declares it. First list the data that has to be stored, then match each part to a type:

    • Enumerated: a small, fixed, ordered list of named values, such as the days of the week.
    • Record: several related items of different data types that belong together, such as the details of one book.
    • Pointer: stores the memory address of another variable or data item, for example to link one node of a linked list to the next.
    • Set: a collection of values with no order and no repeats, where you mainly need to check whether a value is in it.

    Worked example

    A library keeps these details for each book: its ISBN, its title, the date it was added, whether it is on loan, and its genre. The genre is always one of Fiction, Science, History or Art. Declare suitable data types.

    1. Step 1
      The genre can only take four fixed values, so it is an enumerated type.
      TYPE TGenre = (Fiction, Science, History, Art)
    2. Step 2
      The details of one book are different data types that belong together, so they form a record. The ISBN is a STRING, not an INTEGER, because it can start with 0 and is never used in a calculation.
      TYPE TBook
         DECLARE ISBN      : STRING
         DECLARE Title     : STRING
         DECLARE DateAdded : DATE
         DECLARE OnLoan    : BOOLEAN
         DECLARE Genre     : TGenre
      ENDTYPE
    3. Step 3
      To store many books, declare an array of the record type.
      DECLARE Library : ARRAY[1:5000] OF TBook

    Used later in

    19.1Algorithmslinked list

    13.1

    Concepts in 13.1

    Every idea explained in 13.1, and where else in the notes it is used.

    ConceptIn one lineAlso used in
    built-in data typeA data type the language already has, such as INTEGER or STRING.13.1 User-defined data types
    classA composite type that holds data and the procedures that act on it.only here
    composite data typeA data type built from other data types, holding several values, such as a record or set.13.1 Record data types, 13.1 Set data types, 13.1 Classes and objects
    dereferencingReading the value stored at the address a pointer holds.only here
    enumerated data typeA non-composite data type with an ordered list of possible values.13.1 Non-composite and composite types, 13.1 Choosing a data type
    non-composite data typeA data type defined without reference to another data type, such as enumerated or pointer.13.1 Enumerated data types, 13.1 Pointer data types
    pointer data typeA non-composite data type used to reference a memory location.13.1 Non-composite and composite types, 13.1 Choosing a data type
    record data typeA composite data type made of fields, each with its own data type.13.1 Non-composite and composite types, 13.1 Choosing a data type, 13.2 Files, records and fields
    set data typeA composite data type holding values with no order and no repeats, used with set operations.13.1 Non-composite and composite types
    user-defined data typeA data type the programmer designs, built from other data types.13.1 Non-composite and composite types

    When you want more than reading

    The notes are the front door. The class is where it clicks.

    Join a class →Ask on WhatsApp