Chapter 9 Key Takeaways: Arrays

The Big Ideas

  1. Arrays are your first real data structure. They store multiple values of the same type under a single name, accessed by index, in contiguous memory. This enables O(1) random access — reaching any element is equally fast.

  2. Pascal's array indexing is a design strength. You can index arrays by any ordinal type — integers, characters, enumerated types, subranges. Choosing meaningful index ranges (e.g., array[1..12] for months, array['A'..'Z'] for letters) makes code self-documenting.

  3. Always define named array types. Pascal's type compatibility rules require that array parameters match by type name, not structure. TGradeArray and TScoreArray are incompatible even if both are array[1..30] of Integer. This strictness prevents subtle bugs.

  4. The three ways to pass arrays to subprograms: - var — subprogram can modify the array (pass by reference) - const — subprogram can only read the array (safe and efficient) - array of T (open array) — accepts arrays of any size (maximum flexibility)

  5. Five fundamental array algorithms solve most problems: - Linear search (find a value) - Accumulation (sum, average) - Find extremes (max, min, and their indices) - Counting (how many meet a condition) - Reversal (in-place swap from ends to middle)

  6. Multidimensional arrays model grids and matrices. Declare with array[rows, cols], access with arr[r, c], iterate with nested loops (outer = row, inner = column for row-major performance).

  7. Dynamic arrays handle unknown sizes. Declared without bounds, sized with SetLength, always zero-indexed. Resize in chunks to avoid O(n^2) copying costs.

  8. Parallel arrays are a stepping stone, not a destination. They work but are fragile — any operation that reorders one array must simultaneously reorder all others. Records (Chapter 10) are the proper solution.

Common Mistakes to Avoid

  • Forgetting to initialize array elements before reading them
  • Hard-coding bounds instead of using Low, High, and Length
  • Compiling without range checking ({$R+}) during development
  • Accessing a dynamic array before calling SetLength
  • Growing a dynamic array one element at a time in a loop (O(n^2) performance)
  • Assuming open array parameters use the caller's index range (they always start at 0)

Connections

  • Chapter 7 (Subprograms): Arrays make subprograms dramatically more useful — a single function can process any amount of data.
  • Chapter 8 (Enumerated Types): Enumerated types as array indices create self-documenting data structures.
  • Chapter 10 (Records): Records group different types together; arrays of records combine the strengths of both.
  • Chapter 5 (CASE): The case statement pairs naturally with arrays for classification and distribution tasks.

One Sentence Summary

Arrays give us indexed, constant-time access to collections of same-typed values — and when combined with named types, open parameters, and fundamental algorithms, they become the workhorse data structure for the majority of programming problems.