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. What usually goes wrong. What you see What caused it How to fix it --- --- --- ArrayIndexOutOfBoundsException on a grid that is not square The cell was indexed as g[c][r], so a column number was used as a row number Index a

Challenge: Sum a Grid