01
Overview

Row-by-row processing of 2D arrays.

When every problem reduces to "do something to each row," the nested-loop template becomes a power tool.

Most AP-style 2D array problems are row-processing problems in disguise: sum a row, count values in a row, find a row that satisfies some property. The nested-loop pattern is the same; only the inner work changes.

The standard row-major traversal is the template you'll deploy most often. Outer loop walks rows; inner loop walks columns. Whatever happens inside the inner loop defines the problem.

This pattern underlies nearly every grid problem on AP CSA FRQ #4 — so build the template into reflex.

02
Core Concept

One template, many tasks.

Per-row accumulators turn traversal into computation.

The row-major template

for (int r = 0; r < grid.length; r++) {
    // optional: per-row setup (e.g., int rowSum = 0;)
    for (int c = 0; c < grid[r].length; c++) {
        // inner work on grid[r][c]
    }
    // optional: per-row finish (e.g., print rowSum)
}

Summing each row

public static int[] rowSums(int[][] grid) {
    int[] sums = new int[grid.length];
    for (int r = 0; r < grid.length; r++) {
        int sum = 0;
        for (int c = 0; c < grid[r].length; c++) {
            sum += grid[r][c];
        }
        sums[r] = sum;
    }
    return sums;
}

The key trick: reset sum to 0 at the start of every row, inside the outer loop. If you put it before the outer loop, all rows share one accumulator and you get the grand total instead.

Counting values per row

public static int countPositiveInRow(int[][] grid, int row) {
    int count = 0;
    for (int c = 0; c < grid[row].length; c++) {
        if (grid[row][c] > 0) count++;
    }
    return count;
}

To process every row, wrap this with an outer loop and a new array of results.

03
Key Vocabulary

Terms behind every row-processing problem.

Precise vocabulary speeds up MCQ reading.

Vocabulary
  • Row-major traversal — outer loop walks rows; inner loop walks columns.
  • Per-row accumulator — a variable reset for each new row.
  • Row sum — total of all elements in a single row.
  • Row count — how many cells in a row satisfy a condition.
  • Outer / inner loop — the two nested loops in the template.
  • grid.length / grid[r].length — number of rows / columns in row r.
04
AP Exam Focus

How row processing is tested.

Accumulator placement and correct bounds dominate.

Exam Focus

MCQ patterns to expect:

  • Where does sum = 0 belong? Inside the outer loop = per-row sum. Before the outer loop = grand total.
  • Wrong inner bound. The inner loop must use grid[r].length, not grid.length.
  • Returning the right structure. An array of row sums vs a single total are different return types.

FRQ tip: declare and initialize per-row variables at the top of the outer loop. Store results in a new array of length grid.length.

05
Worked Example

Return per-row counts of even numbers.

Convert a row-processing description into the template.

Worked Example AP-style reasoning

Problem. Given int[][] grid, return an int[] where index r holds the number of even values in row r.

Step 1. Create the result array, sized to grid.length.

Step 2. For each row, reset a per-row counter to 0. Walk the columns; increment on even values.

Step 3. Store the counter into the result array.

public static int[] evenCounts(int[][] grid) {
    int[] result = new int[grid.length];
    for (int r = 0; r < grid.length; r++) {
        int count = 0;
        for (int c = 0; c < grid[r].length; c++) {
            if (grid[r][c] % 2 == 0) {
                count++;
            }
        }
        result[r] = count;
    }
    return result;
}

Trace. For {1, 2, 4}, {3, 5, 7}, {6, 8, 9}: row 0 has 2 evens, row 1 has 0, row 2 has 2. Returns {2, 0, 2}.

Why this matters. Sum, count, max-per-row, and string-build-per-row all share this exact skeleton. Once you can write it from memory, half of FRQ #4 is solved.

06
Common Mistakes

Row-processing pitfalls.

All of these are about where code lives.

Watch Out
  • Resetting in the wrong place. Reset per-row accumulators inside the outer loop, not before it.
  • Storing the result after the inner loop ends — but on the wrong row index. Use r, not r - 1.
  • Wrong inner bound. Always grid[r].length.
  • Wrong return type. Per-row results require an array; a single total returns one value.
  • Using enhanced-for and losing the row index. If you need r, use an indexed outer loop.
07
Reference Table

Row processing at a glance.

Where each part of the template lives.

Row Processing Reference

AP Quick Reference
Code Location What goes here AP Exam Tip
Before outer loop Result array, grand totals Lives across all rows.
Top of outer loop Per-row variables (reset) Fresh value for each row.
Inside inner loop Cell-by-cell work Access via grid[r][c].
After inner loop Save row's result result[r] = ...;
After outer loop Return / final action All rows processed.
08
Practice Tip

How to lock in row processing.

Master one template; vary the inner work.

Practice Strategy

Use the three practice tools below this lesson in order:

  • MCQ Practice — for each option, ask "where is the accumulator reset?" Many traps hinge on that single line.
  • Java Lab — write methods that return per-row sums, counts, maxes, and joined Strings. Build them from the same template.
  • FRQ Practice — start every grid FRQ by writing the row-major template, then fill in the inner work.
09
Section · 09

Practice — attempt these now.

AP-style assessments aligned to this lesson. Time them.

Topic Quiz 40 min 40 marks Pending

AP CSA Topic Practice: 2D Traversal and Row Processing

AP-style topic practice assessment

Start
FRQ Practice 30 min 18 marks Pending

AP CSA Unit 05 Topic 01 2D Traversal and Row Processing FRQ Practice

AP-style topic practice assessment

Start