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
DECLARE MyPointer : TIntPointer
DECLARE MyNumber: INTEGER

MyNumber ← MyPointer^
//Accessing the value pointed by MyPointer

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
PythonPseudocode
# Record Declaration class <identifier>: def __init__(self, i1, i2): self.i1 = i1 self.i2 = i2 ……… Example – Declaration of Record data type This user-defined data type holds data about a student. class StudentRecord: def __init__(self, LastName, FirstName, DateOfBirth, YearGroup, FormGroup): self.LastName = LastName self.FirstName = FirstName self.DateOfBirth = DateOfBirth self.YearGroup = YearGroup self.FormGroup = FormGroup Example – using user-defined data types Form = [0] * 30 Pupil1 = StudentRecord() Pupil1.LastName = "Johnson" Pupil1.Firstname = "Leroy" Pupil1.DateOfBirth = 02/01/2005 Pupil1.YearGroup = 6 Pupil1.FormGroup = ꞌAꞌ Pupil2 = Pupil1 for index in range(1, 30): Form[Index].YearGroup ← Form[Index].YearGroup + 1Record Declaration: TYPE <identifier1> DECLARE <identifier2> : <dataType> DECLARE <identifier3> : <dataType> ... ENDTYPE Example – Declaration of Record data type This user-defined data type holds data about a student. TYPE StudentRecord DECLARE LastName : STRING DECLARE FirstName : STRING DECLARE DateOfBirth : DATE DECLARE YearGroup : INTEGER DECLARE FormGroup : CHAR ENDTYPE Example – using user-defined data types DECLARE Pupil1 : StudentRecord DECLARE Pupil2 : StudentRecord DECLARE Form : ARRAY[1:30] OF StudentRecord Pupil1.LastName ← "Johnson" Pupil1.Firstname ← "Leroy" Pupil1.DateOfBirth ← 02/01/2005 Pupil1.YearGroup ← 6 Pupil1.FormGroup ← ꞌAꞌ Pupil2 ← Pupil1 FOR Index ← 1 TO 30 Form[Index].YearGroup ← Form[Index].YearGroup + 1 NEXT Index

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.
DECLARE Pupil1 : StudentRecord
DECLARE Pupil2 : StudentRecord
DECLARE Form : ARRAY[1:30] OF StudentRecord
DECLARE ThisSeason : Season
DECLARE NextSeason : Season
DECLARE MyPointer : TIntPointer

Pupil1.LastName ← "Johnson"
Pupil1.Firstname ← "Leroy"
Pupil1.DateOfBirth ← 02/01/2005
Pupil1.YearGroup ← 6
Pupil1.FormGroup ← ꞌAꞌ

Pupil2 ← Pupil1

FOR Index ← 1 TO 30
Form[Index].YearGroup ← Form[Index].YearGroup + 1
NEXT Index

ThisSeason ← Spring
MyPointer ← ^ThisSeason
NextSeason ← MyPointer^ + 1
// access the value stored at the memory address