9618 Computer Science - Data Representation
User-defined data types allow programmers to create custom data structures that exactly match a program's requirements. They are built using primitive data types provided by a programming language, or other data types that have been previously defined. This makes programs much easier to code, read, and debug!
User-defined data types are custom data structures created by programmers to match the specific needs of a program. They are built using primitive data types (such as INTEGER, REAL, STRING, BOOLEAN) or other data types that have been previously defined in a program.
A user-defined data type is a data type based on an existing data type or other data types that have been defined by a programmer. User-defined data types can make programs much easier to code, read, and debug.
| Category | Description | Examples |
|---|---|---|
| Non-Composite | Made up of a single data item; defined without referencing another data type | Enumerated types, Pointer types |
| Composite | Made up of multiple data items, often of different types; references other data types in its definition | Records, Sets, Classes/Objects |
Remember: Non-Composite = Single data type (no references to other types), while Composite = Multiple data types (contains references to other types in its definition). A record is composite because it references other types like STRING and INTEGER!
Enumerated data type is a non-composite user-defined data type that defines a list of all possible values with an implied order. If you are using lots of constants in your program that are all related to each other, it is a good idea to keep them together using an enumerated type.
Pseudocode Syntax:
TYPE= (value1, value2, value3, ...)
TYPE TDays = (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday) DECLARE Today : TDays DECLARE Yesterday : TDays DECLARE Tomorrow : TDays Today ← Wednesday Yesterday ← Today - 1 // Sets to Tuesday Tomorrow ← Today + 1 // Sets to Thursday
TYPE TMonth = (January, February, March, April, May, June,
July, August, September, October, November, December)
DECLARE thisMonth : TMonth
DECLARE nextMonth : TMonth
thisMonth ← January
nextMonth ← thisMonth + 1 // nextMonth is now February
Type names usually begin with T to aid the programmer (e.g., TDays, TMonth). Remember: enumerated values are NOT strings - they don't use quotation marks! The implied order means you can use arithmetic like Today + 1 to get the next value.
Pointer data type is a non-composite user-defined data type that stores the memory address of where data is stored, rather than the data itself. Pointers are used to reference a memory location and are essential for dynamic data structures like linked lists.
Pseudocode Syntax:
TYPE= ^ // ^ indicates pointer to TypeName
// Define pointer type TYPE TIntegerPointer = ^INTEGER // Declare pointer variable DECLARE MyIntegerPointer : TIntegerPointer // Declare ordinary variables DECLARE Number1, Number2 : INTEGER Number1 ← 100 // Store address of Number1 in pointer MyIntegerPointer ← @Number1 // @ gets the address // Dereference: get value at the address Number2 ← MyIntegerPointer^ * 2 // Number2 becomes 200
TYPE TMonth = (January, February, March, ...) TYPE TMonthPointer = ^TMonth // Pointer to TMonth DECLARE monthPointer : TMonthPointer DECLARE thisMonth : TMonth DECLARE myMonth : TMonth thisMonth ← March monthPointer ← @thisMonth // Pointer now holds address // Dereferencing - get value at address myMonth ← monthPointer^ // myMonth becomes March
TYPE TPtr = ^INTEGER)MyPointer^ gets the value)@Number1)Record data type is a composite user-defined data type that contains a fixed number of components (fields), which can be of different data types. Records allow the programmer to collect together related values with different data types into a single structure.
Pseudocode Syntax:
TYPEDECLARE : DECLARE : ... ENDTYPE
TYPE EmployeeRecord
DECLARE EmployeeFirstName : STRING
DECLARE EmployeeFamilyName : STRING
DECLARE DateEmployed : DATE
DECLARE Salary : CURRENCY
ENDTYPE
DECLARE Employee1 : EmployeeRecord
// Assign values using dot notation
Employee1.EmployeeFirstName ← "John"
Employee1.EmployeeFamilyName ← "Smith"
Employee1.DateEmployed ← #16/05/2017#
Employee1.Salary ← 45000.00
// Output a field
OUTPUT Employee1.EmployeeFirstName
TYPE StudentType
DECLARE Name : STRING
DECLARE DateOfBirth : DATE
DECLARE Height : REAL
DECLARE NumberOfSiblings : INTEGER
DECLARE IsFullTimeStudent : BOOLEAN
ENDTYPE
// Declare single record
DECLARE Person : StudentType
Person.Name ← "Fred"
Person.NumberOfSiblings ← 3
Person.IsFullTimeStudent ← TRUE
// Declare array of records
DECLARE Students : ARRAY[1:100] OF StudentType
Students[1].Name ← "Fred"
OUTPUT Students[1].Name
Records are the most useful and widely used composite data type! They allow you to represent real-world entities like students, products, or customers. Remember to use dot notation to access individual fields: RecordName.FieldName
Set data type is a composite user-defined data type that represents an unordered collection of unique elements. Sets are useful for situations where duplication is not allowed and where membership testing is common.
Pseudocode Syntax:
TYPE= SET OF DEFINE (value1, value2, value3, ...) :
TYPE SLetter = SET OF CHAR
DEFINE vowels ('a', 'e', 'i', 'o', 'u') : SLetter
// Membership testing
IF 'e' ISIN vowels THEN
OUTPUT "'e' is a vowel"
ENDIF
TYPE ColourSet = SET OF STRING
DEFINE Colours ("Red", "Green", "Blue") : ColourSet
IF "Green" ISIN Colours THEN
OUTPUT "Green is in the set"
ENDIF
| Operation | Symbol | Description |
|---|---|---|
| Union | ∪ or | | All elements from both sets |
| Intersection | ∩ or & | Elements common to both sets |
| Difference | − or − | Elements in first set but not in second |
| Symmetric Difference | △ or ^ | Elements in either set but not in both |
| Membership | ISIN | Check if element exists in set |
Think of sets like a club membership list: Each person can only be on the list once (unique), the order doesn't matter (unordered), and you can check if someone is a member (ISIN), add new members, or remove members!
Class is a composite data type that includes variables of given data types (attributes/properties) and methods (code routines that can be run by an object in that class). An object is defined from a given class - several objects can be defined from the same class.
| Feature | Record | Class |
|---|---|---|
| Contains | Only data fields | Data fields AND methods |
| Purpose | Group related data | Define objects with behavior |
| Usage | Direct variable declaration | Object instantiation |
| Example | Student record with Name, Age | Car class with color, model, drive() |
A Car class might include:
Objects created from this class: myCar, yourCar, companyCar - each has its own color, model, etc.
Classes and objects are fundamental to Object-Oriented Programming (OOP). They will be covered in more depth in Chapter 20 (Further Programming). For now, remember that a class is a user-defined data type that combines data AND methods!
| Data Type | Category | Description | Use Case |
|---|---|---|---|
| Enumerated | Non-Composite | List of named, fixed values with implied order | Days of week, months, directions |
| Pointer | Non-Composite | Stores memory address of data | Dynamic structures, linked lists |
| Record | Composite | Groups related fields of different types | Student data, employee records |
| Set | Composite | Unordered collection of unique elements | Vowels, colours, user IDs |
| Class | Composite | Blueprint with data AND methods | Objects with behavior |
Answer:
Additional points for deeper understanding: User-defined types improve code readability and make programs easier to debug by restricting values to meaningful options.
Answer:
Answer:
Answer:
TYPE Animal
DECLARE AnimalName : STRING
DECLARE AnimalAge : INTEGER
DECLARE NumberInZoo : INTEGER
DECLARE LocationInZoo : STRING
ENDTYPE
Additional notes: Type name should start with T convention (e.g., TAnimal), and field names should be descriptive. Each field must have an appropriate data type.
Answer:
TYPE TDirection = (North, East, South, West) DECLARE CurrentDirection : TDirection CurrentDirection ← North // Can also use ordinal operations CurrentDirection ← CurrentDirection + 1 // Now East
Additional points: Values are ordinal, so you can use +1, -1 operations. Values are NOT strings and do not use quotation marks.
Answer:
TYPE TIntPointer = ^INTEGER DECLARE ptr : TIntPointer DECLARE num : INTEGER num ← 100 ptr ← @num // ptr now holds address of num OUTPUT ptr^ // Dereference: outputs 100
Answer:
Key distinction: A record is composite because it references STRING, INTEGER, etc. in its definition. An enumerated type is non-composite because it only defines a list of values, not referencing any other type.
Answer:
TYPE SVowel = SET OF CHAR
DEFINE vowels ('a', 'e', 'i', 'o', 'u') : SVowel
IF 'e' ISIN vowels THEN
OUTPUT "It's a vowel"
ENDIF
Answer:
TYPE TStudent
DECLARE Name : STRING
DECLARE Age : INTEGER
DECLARE Grade : STRING
ENDTYPE
DECLARE Students : ARRAY[1:50] OF TStudent
// Assign values to first student
Students[1].Name ← "Alice"
Students[1].Age ← 17
Students[1].Grade ← "A"
// Output first student's name
OUTPUT Students[1].Name
Key points: Use dot notation with array index to access fields. The record type groups related data together efficiently.
Answer:
Answer:
Example: A Student record has Name, Age, Grade. A Student CLASS would also have methods like CalculateGPA() or Promote().
A data type based on an existing data type or other data types that have been defined by a programmer to match specific program requirements.
A data type that can be defined without referencing another data type. It contains only one data item. Examples: enumerated types, pointer types.
A data type that references other data types in its definition. It contains multiple data items, often of different types. Examples: records, sets, classes.
A non-composite data type defined by a given list of all possible values that has an implied order. Values are not strings and are ordinal.
A non-composite data type that uses the memory address of where data is stored. It is a form of indirect referencing.
The process of accessing the value stored at the memory location that a pointer is pointing to. Done using the ^ symbol in pseudocode.
A composite data type that contains a fixed number of components (fields), which can be of different data types. Fields are accessed using dot notation.
A composite data type that represents an unordered collection of unique elements. Supports mathematical operations like union, intersection, and difference.
A composite data type that includes variables of given data types (attributes) and methods (code routines). It is a blueprint for creating objects.
An instance of a class. Multiple objects can be created from the same class, each having its own copy of the class attributes.
The method of accessing individual fields of a record using the format RecordName.FieldName (e.g., Student.Name).
Having an implied order. Enumerated values are ordinal, meaning they can be used with arithmetic operations like +1 and -1.
WRONG: TYPE TDays = ("Monday", "Tuesday")WRONG: StudentName ← "John"@ gets address, ^ dereferences| Situation | Best Type | Why |
|---|---|---|
| Fixed list of related values | Enumerated | Restricts to valid options |
| Working with memory addresses | Pointer | Indirect referencing |
| Grouping related data | Record | Different types together |
| Unique elements only | Set | No duplicates allowed |
| Data with behaviors | Class | Methods included |
User-defined data types are essential tools for creating well-structured, maintainable programs. They allow you to model real-world entities accurately and make your code easier to understand. Always choose the appropriate type based on your data requirements!