Chapter 9 Key Takeaways: Arrays
The Big Ideas
-
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.
-
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. -
Always define named array types. Pascal's type compatibility rules require that array parameters match by type name, not structure.
TGradeArrayandTScoreArrayare incompatible even if both arearray[1..30] of Integer. This strictness prevents subtle bugs. -
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) -
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)
-
Multidimensional arrays model grids and matrices. Declare with
array[rows, cols], access witharr[r, c], iterate with nested loops (outer = row, inner = column for row-major performance). -
Dynamic arrays handle unknown sizes. Declared without bounds, sized with
SetLength, always zero-indexed. Resize in chunks to avoid O(n^2) copying costs. -
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, andLength - 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
casestatement 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.