2D Array Algorithms in AP CSA: Row Sums, Column Sums, and Matrix Patterns (2026)
2D Array Algorithms in AP CSA: Row Sums, Column Sums, and Matrix Patterns
FRQ 4 on the AP CSA exam almost always involves a 2D array. This page covers the exact algorithmic patterns you need: summing rows and columns, finding extremes, counting with conditions, and processing subregions of a matrix.
📄 On This Page
📈 Row Sum Pattern
To compute the sum of a single row, hold the row index constant and iterate across columns:
public static int rowSum(int[][] grid, int row)
{
int sum = 0;
for (int c = 0; c < grid[row].length; c++)
{
sum += grid[row][c];
}
return sum;
}
To get ALL row sums, wrap this in an outer loop:
int[] rowSums = new int[grid.length];
for (int r = 0; r < grid.length; r++)
{
for (int c = 0; c < grid[r].length; c++)
{
rowSums[r] += grid[r][c];
}
}
The exam uses grid.length for the number of rows and grid[0].length for the number of columns. Mixing these up is the #1 source of errors on 2D array questions.
📉 Column Sum Pattern
Column sums flip the loop order. Hold the column constant, iterate down the rows:
public static int colSum(int[][] grid, int col)
{
int sum = 0;
for (int r = 0; r < grid.length; r++)
{
sum += grid[r][col];
}
return sum;
}
grid.length (rows) and the column index is fixed. Students often write grid[col].length by accident, which causes either wrong results or an exception.
🔎 Finding Max/Min in a Matrix
public static int findMax(int[][] grid)
{
int max = grid[0][0];
for (int r = 0; r < grid.length; r++)
{
for (int c = 0; c < grid[r].length; c++)
{
if (grid[r][c] > max)
{
max = grid[r][c];
}
}
}
return max;
}
Critical: Initialize max to grid[0][0], not to 0. If all values are negative, initializing to 0 gives a wrong answer. Same logic applies to finding the minimum.
🔢 Counting with Conditions
// Count how many elements are greater than a threshold
public static int countAbove(int[][] grid, int threshold)
{
int count = 0;
for (int r = 0; r < grid.length; r++)
{
for (int c = 0; c < grid[r].length; c++)
{
if (grid[r][c] > threshold)
{
count++;
}
}
}
return count;
}
This pattern appears on nearly every FRQ 4. The condition inside the if statement changes, but the nested loop structure stays the same.
❌ Common Pitfalls
grid.length = number of rows. grid[0].length = number of columns. Using the wrong one is the most common 2D array mistake.
Always initialize to the first element grid[0][0], not to 0 or Integer.MAX_VALUE. The exam will test arrays where all values are negative.
If the algorithm needs the row or column index (like finding WHERE the max is), you must use a standard for loop. Enhanced for only gives you the value.
✏ Practice Questions
int[][] m = {{3,1},{4,2},{5,6}}, what does rowSum(m, 1) return?grid.length return for int[][] grid = new int[4][7]?int max = 0 then searches a 2D array for the maximum. The array contains {{-5,-3},{-8,-1}}. What does the method incorrectly return?grid?int[][] data = {{1,2,3},{4,5,6},{7,8,9}}What is the value of
data[data.length-1][0] + data[0][data[0].length-1]?rowSums. What is rowSums.length?I. Uses
grid.length for outer loop bound and grid[r].length for innerII. Uses
== comparison for int valuesIII. Initializes count to 1
❓ Frequently Asked Questions
What 2D array algorithms appear on the AP CSA exam?
The exam tests row-sum and column-sum calculations, finding the maximum or minimum value in a 2D array, counting elements meeting a condition, and traversing in row-major vs column-major order.
How do I sum a single row in a 2D array?
Use a single for loop iterating across columns: for (int c = 0; c less than grid[row].length; c++) and accumulate grid[row][c]. The row index stays fixed while the column index changes.
What is the difference between row-major and column-major traversal?
Row-major: outer loop iterates rows, inner loop iterates columns. Processes left-to-right, top-to-bottom. Column-major: outer loop iterates columns, inner loop iterates rows. Processes top-to-bottom, left-to-right.
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]