What Does This Program Do? - Arrays

Contest 3 adds arrays to WDTPD problems. You’ll trace programs that create, read, modify, and search arrays. The core skill is tracking array contents alongside loop variables.

ACSL array syntax

Arrays in ACSL pseudocode use parentheses and are zero-indexed (first element is at index 0).

A(0) = 5
A(1) = 3
A(2) = 8

Or initialized all at once:

A(0)=5: A(1)=3: A(2)=8: A(3)=1: A(4)=4

Accessing: gives the value at index 2 (which is in the example above).

Note: ACSL uses parentheses for arrays, like . Some problems and textbooks use brackets instead - they mean the same thing. In this lesson we use both notations interchangeably.

Length: Some problems use to get the number of elements.

Try it: trace an array program

Watch how the array A is accessed and modified using index variables.

Code
1A(0)=5: A(1)=3: A(2)=8: A(3)=12sum = 03FOR i = 0 TO 3 STEP 14    sum = sum + A(i)5NEXT6OUTPUT: sum
Variables
NameValue
A(0)5
A(1)3
A(2)8
A(3)1
Step 1 of 11

The array table method

When tracing array programs, draw a table with one column per array index and update it as the program runs.

Example:

A = [4, 7, 2, 9, 1]
FOR i = 0 TO 3
    IF A[i] > A[i+1] THEN
        temp = A[i]
        A[i] = A[i+1]
        A[i+1] = temp
    END IF
NEXT
OUTPUT: A
iCompareSwap?Array after
---
? NoNo
? YesYes
? NoNo
? YesYes

Answer: 4 2 7 1 9

Recognize the pattern? This is one pass of bubble sort - it moves the largest element to the end.

Common array patterns in ACSL

Pattern 1: Summing elements

A = [3, 1, 4, 1, 5]
s = 0
FOR i = 0 TO 4
    s = s + A[i]
NEXT
OUTPUT: s

Answer: (just add them all up)

Pattern 2: Counting elements meeting a condition

A = [3, 1, 4, 1, 5, 9, 2, 6]
count = 0
FOR i = 0 TO 7
    IF A[i] > 3 THEN
        count = count + 1
    END IF
NEXT
OUTPUT: count

Answer: (the elements , , , are )

Pattern 3: Finding max/min

A = [3, 7, 1, 9, 4]
max = A[0]
pos = 0
FOR i = 1 TO 4
    IF A[i] > max THEN
        max = A[i]
        pos = i
    END IF
NEXT
OUTPUT: max, pos

Answer: (maximum is at index )

Pattern 4: Reversing an array

A = [1, 2, 3, 4, 5]
FOR i = 0 TO 1
    temp = A[i]
    A[i] = A[4 - i]
    A[4 - i] = temp
NEXT
OUTPUT: A
iSwapArray
swap and
swap and

Answer: 5 4 3 2 1

Note: the loop only goes to index 1 (halfway). Going further would swap them back.

Pattern 5: Building a new array from an existing one

A = [2, 0, 1, 4, 3]
B = [0, 0, 0, 0, 0]
FOR i = 0 TO 4
    B[A[i]] = i
NEXT
OUTPUT: B

This creates an inverse permutation - a reverse lookup table:

iA[i]Assignment

Answer: 1 2 0 4 3

2D arrays

Some problems use 2D arrays (grids/matrices). Access syntax: or .

Example:

A = [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]

s = 0
FOR i = 0 TO 2
    s = s + A[i][i]
NEXT
OUTPUT: s

This sums the diagonal:

Array index arithmetic

ACSL loves expressions as array indices. Trace carefully.

Example: What does mean?

If A = [3, 0, 1, 2] and :

So .

This is a common source of errors - always evaluate the inner expression first.

Common mistakes

  1. Off-by-one with zero indexing. An array of elements has indices -, not -.
  2. Forgetting the array changes mid-loop. After a swap, subsequent iterations see the new values, not the original.
  3. Nested array access. - evaluate inside out. Get first, then use that as the index.
  4. 2D array row vs column confusion. - first index is the row, second is the column.

Contest strategy

  • Draw the array contents and update them as you trace
  • For large arrays, only track the elements that change
  • Recognize common patterns (sort, search, reverse) to predict outputs faster
  • Watch index arithmetic carefully - this is where most errors happen
  • Typical time: 90-120 seconds per problem