FRQ Type 4: 2D Array

2d array

Part of: Exam Workshop: FRQ Strategies

What This FRQ Asks The last FRQ works with a two-dimensional array, a grid of rows and columns. You might sum the whole grid, sum one row, find a column total, or count values that match a rule. The rubric rewards a correct nested loop with the row and column bounds set right. The Nested Loop A 2D array in Java is an array of rows. For a grid named g, g.length is the number of rows, and g[r].length is the number of columns in row r. The standard traversal is an outer loop over rows and an inner loop over columns. Read g[r][c] as row r, column c. The most common mistake is swapping the two, writing g[c][r], which goes out of bounds on a non-square grid. Keep row first, column second, every time. Row Versus Column Work If the prompt asks about one specific row, you only need a single loop over the columns of that row: A column total is the opposite: fix the column and loop over the rows, using g[r][col]. Decide which index stays fixed before you write the loop. Key points: - g.length counts rows, g[r].length counts columns. - Always index as g[row][column], row first. - For one row, loop columns; for one column, loop rows.

Challenge: Sum a Grid