Define and use composite data types

Resources | Subject Notes | Computer Science

13.1 User-defined Data Types

In computer science, data types define the kind of values a variable can hold and the operations that can be performed on those values. While programming languages often provide built-in data types (like integers, floating-point numbers, characters, and strings), we can also define our own data types. These are known as user-defined data types, and a common example is composite data types.

Composite Data Types

Composite data types are data types that are constructed from other data types. They allow us to group together multiple values of potentially different types into a single unit. This can make our code more organized, readable, and easier to manage. Common examples include arrays, records (or structures), and unions.

Arrays

An array is a collection of elements of the same data type, stored in contiguous memory locations. Arrays are indexed, meaning we access individual elements using an index (usually starting from 0). Arrays are useful for storing lists of similar items.

Example (in pseudo-code):

type IntegerArray is array of integers of size N
variable my_array: IntegerArray of size 5
my_array[0] = 10
my_array[1] = 20
my_array[2] = 30
my_array[3] = 40
my_array[4] = 50

Records (Structures)

A record (also known as a structure in some languages) is a collection of variables of potentially different data types, grouped together under a single name. Each variable within a record is called a member or field. Records are useful for representing complex objects with multiple attributes.

Example (in pseudo-code):

type Student is record
    name: string
    student_id: integer
    gpa: float
end record
variable student1: Student
student1.name = "Alice"
student1.student_id = 12345
student1.gpa = 3.8

Unions

A union is a data type that can hold one of several different data types. The type of the data stored in a union is known at runtime. Unions are less commonly used than records, but they can be useful when you need to store different types of data in the same memory location, but only one at a time.

Example (in pseudo-code):

type Data is union of integer, string, float
variable data1: Data
data1.integer_value = 10
data1.string_value = "Hello"

Table summarizing Composite Data Types

Data Type Description Example Use Case
Array Collection of elements of the same data type, stored contiguously. Array of integers: {1, 2, 3, 4, 5} Storing lists of items, implementing stacks and queues.
Record (Structure) Collection of variables of potentially different data types, grouped together. Student record: {name="Alice", student_id=12345, gpa=3.8} Representing objects with multiple attributes, such as students, products, or employees.
Union Data type that can hold one of several different data types. Union holding an integer and a string. Storing data where the type is not known in advance, or when memory is constrained.

Advantages of User-Defined Data Types

  • Improved Organization: Grouping related data together makes code easier to understand and maintain.
  • Data Abstraction: Hides the internal details of data representation, providing a simpler interface to the data.
  • Code Reusability: User-defined data types can be reused throughout a program, reducing code duplication.
  • Type Safety: Helps to prevent errors by ensuring that data is accessed and manipulated in the correct way.