A2 DataTypes Lecture 1 (Enumerated, Pointers etc)


Data representation: numbers and basic data handling

  • Data representation starts with numbers: floating point numbers, whole numbers (integers), positive and negative values, and decimal numbers.

  • You will learn to create your own data types (user-defined data types) in addition to the built-in types.

  • There is a focus on data organization and access patterns, including file organization, reading and writing (Open file, Close file), and data progression (sequential storage) versus random access.

Specific data-type concepts: built-in, user-defined, and composite vs noncomposite

  • The course differentiates built-in (predefined) data types from user-defined data types that you create to meet specific requirements.

  • Built-in data types are those that are provided by the language (e.g., integers, booleans, characters, strings in some implementations).

  • User-defined data types are constructed from built-in types or from other user-defined types when needed to fulfill a specific functionality the language does not directly provide.

  • Composite data types are formed by combining different data types (e.g., records/structs).

  • Noncomposite data types are simple and do not reference other data types; examples include enumerated types and pointers in this course’s framing.

Built-in vs user-defined data types: foundational ideas

  • Built-in data types exist by default in the language; user-defined types allow you to tailor data structures to your domain.

  • You can build a new data type from built-in types, and sometimes you can build from other user-defined types as well.

  • The lecture emphasizes that user-defined data types are used when there is a functional or definitional need that built-in types cannot satisfy on their own.

Composite data types: records and examples

  • A composite data type is a record/struct that contains multiple fields, potentially of different data types.

  • Example: A student record may include fields such as first name (String), last name (String), absence (Integer), and class (String or Integer).

  • Declaration pattern (pseudocode) often uses a multi-line form with an end marker:

    • Type StudentRecord = Record
      firstName: String
      lastName: String
      absence: Integer
      class: String
      End Record

  • These are designed to model real-world entities by aggregating related data into a single unit.

Enumerated data types: ordered value lists

  • Enumerated data types are defined by an ordered list of values. The order matters for comparisons and sequencing.

  • Example: Days of the week or months of the year.

    • Example declaration (pseudocode):
      Type DayOfWeek = (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)

  • These are noncomposite data types because they consist of a fixed set of values with a defined order.

  • You can use enumerated values in conditional logic, and their order enables comparison, e.g., today > Friday.

Practical example: days of the week and simple comparisons

  • Example scenario: declare today as a DayOfWeek and set today = Tuesday.

  • If today > Friday, then some condition evaluates to True; otherwise, False.

  • This demonstrates how enumerated types support ordering and comparison operations.

  • Representative expression (informal):

    • Today: DayOfWeek := Tuesday

    • If today > Friday then output True else output False.

    • In this example, Tuesday is considered less than Friday, so the condition yields False.

Pointers: concept, storage, and dereferencing

  • A pointer data type stores addresses (references) to other memory locations rather than actual values.

  • The concept is crucial for understanding by-reference data manipulation and indirect addressing.

  • In the lecture, a pointer type is introduced as a noncomposite data type whose value is an address.

  • Example concepts (as discussed in class):

    • Declare an integer pointer: pointer to Integer

    • A variable holds a value (e.g., 5) and has an address in memory (e.g., A2).

    • The address-of operator (often written as @ or & in various languages) yields the address of a variable, e.g., address_of(myNumber).

    • The pointer stores this address (e.g., A2).

    • Dereferencing a pointer retrieves the value at the address, e.g., *pointerName yields 23.

    • Indirect addressing is when you access a value by following a pointer to its address, then retrieving the value from that address.

  • Pointers allow you to implement references and dynamic data structures, and they are foundational for understanding memory management and efficient parameter passing.

Pseudocode exercise: pointer to store string references

  • Task: create a pointer data type to store string references and implement the following diagram in pseudocode:

    • Store a reference to a name in a pointer, then dereference to obtain the name.

  • Suggested solution form (per the lecture):

    • Define a pointer type to String:
      Type TMyPointer = ^String

    • Declare variables:
      Var myName: String
      Var yourName: String
      Var username: TMyPointer

    • Store addresses and dereference:
      username := address_of(myName)
      dereferencedName := dereference(username) // yields the value of myName

  • Important note: you should not manually assign addresses to the variables; the address should be obtained via the address operator, and dereferencing should yield the original value.

  • This exercise reinforces the distinction between values, addresses, and dereferenced values, plus the concepts of by-value versus by-reference semantics.

Key takeaways: data types, organization, and connections

  • Data types are split into built-in and user-defined: built-in types exist in the language; user-defined types are created to meet specific needs.

  • Composite data types (like records) combine multiple fields of possibly different types into a single unit.

  • Noncomposite data types include simple, standalone types like enumerations and pointers, which do not store values of other data types in the sense of a structured field.

  • Enumerated data types provide an ordered set of discrete values, enabling straightforward comparisons and sequential reasoning.

  • Pointers are a powerful mechanism for memory addressing and indirect data access, enabling by-reference parameter passing and dynamic data structures.

  • The syllabus emphasizes both theoretical understanding (types, organization, protocols) and practical skills (pseudocode, pointer manipulation, and programming problems).

Quick recap of notation and examples to remember

  • Enumerated type example:
    DayOfWeek=(Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday)\text{DayOfWeek} = (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)

  • Example usage: declare today: DayOfWeek; today := Tuesday; if today > Friday then …

  • Pointer type example (per lecture):

    TYPE TMyPointer = ^STRING
    DECLARE myName,yourName:STRING\text{DECLARE }myName,yourName:STRING
    DECLARE username:TMyPointer\text{DECLARE }username:TMyPointer

    myName ← “Ali”

    yourName ← “Ahmed”


    username← @myName

    yourName ← username^

    fordereferencing:usernamedereferencing: username ^


  • Helped mappings for 3 major ideas:

    • Built-in data types are provided by the language.

    • User-defined data types are created from existing types when needed.

    • Composite data types merge multiple values into one logical unit (records). Noncomposite types include enumerations and pointers.