Subtopic Notes

13.1 - User-defined data types

13. Data Representation

  • Allow programmers to model real-world entities by grouping related data into one structure
  • Used when no suitable built-in data type exists
  • Let programmers create new types that meet specific application needs
  • Derived from one or more existing data types
  • Extend the functionality of built-in data types
  • Improve code readability, reusability, and organization in large programs
  • Make programs more specific to their intended purpose

Non-composite data type

A non-composite data type is a basic data type that holds a single, indivisible value.
Examples: INTEGER, REAL, BOOLEAN, CHARACTER, STRING, Enumerated, Pointers

Enumerated

A user-defined non-composite data type with a list of possible values is called an enumerated data type. The enumerated type should be declared as follows:
TYPE <indentifier> = (value1, value2, value3, ...)

Example
TYPE Season = (Spring, Summer, Autumn, Winter)
DECLARE Season1 : Season
Season1 ← Autumn

Eg. Question
Write pseudocode statements to declare the set data type EvenNumbers to hold this set of even numbers between 2 and 12:
TYPE Numbers = SET OF INTEGER
DEFINE EvenNumbers (2, 4, 6, 8, 10, 12): Numbers

Pointer

A user-defined non-composite data type referencing a memory location is called a pointer. The pointer should be declared as follows:

TYPE <identifier> = ^<data type>

The ^ shows that the variable is a pointer and the data type indicates the type of the data stored in the memory location.

Example – Declarations of pointer type
TYPE TIntPointer = ^INTEGER
TYPE TCharPointer = ^CHAR

Declaration of a variable of pointer type does not require the ^ (caret) symbol to be used.

Example – Declaration of a pointer variable

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode

Example – Accessing address from a normal variable
Test ← @MyNumber
//@MyNumber returns the address of variable MyNumber

Composite data type

A composite data type is a collection of data that can consist of one or more data types, grouped under one identifier.
Examples: Array, List, Set, Collection, Class, Stack, Queue, Linked List, Dictionary, Record, Class, Object

Record Structure

  • Record structure is used to hold a set of different data types under one identifier
  • Python uses object and classes instead of record structure

Record Declaration

Python

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

Pseudocode

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode

Example – Declaration of Record data type

This user-defined data type holds data about a student.

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode

Class: Just like record structure, classes define the structure of the record, specifying what data it holds. It might also include functions and methods.

Object: Specific instance of that class, storing actual values.

Set

A set is a built-in data structure that stores an unordered collection of unique elements.
TYPE <identifier1> = SET OF <data type>
DEFINE <identifier2> (value1, value2, value, … ) : <identifier1\>

Example – Declaration of Set data type
This user-defined data type holds data about vowels.
TYPE LetterSet = SET OF CHAR
DEFINE Vowels ('A','E','I','O','U'): LetterSet

Using user defined data type

When a user-defined data type has been defined it can be used in the same way as any other data type in declarations.

Variables of a user-defined data type can be assigned to each other. Individual data items are accessed using dot notation.

Example – using user-defined data types
This pseudocode uses the user-defined types StudentRecord, Season and TIntPointer defined in the previous section.

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode