4.13 Implementing 2D array algorithms2D algorithms: rows, columns and neighbors

To work on each row separately, reset your sum or counter inside the outer loop, at the start of each row. To work on one column, hold c fixed and let only r move. To work on the whole grid, keep one variable outside both loops.
1public static int[] rowTotals(int[][] g)2{3 int[] totals = new int[g.length];14 for (int r = 0; r < g.length; r++)5 {6 for (int c = 0; c < g[r].length; c++)7 {8 totals[r] += g[r][c];29 }10 }11 return totals;12}1314public static int colMax(int[][] g, int c)15{316 int best = g[0][c];317 for (int r = 1; r < g.length; r++)18 {19 if (g[r][c] > best)20 {21 best = g[r][c];22 }23 }24 return best;25}
- line 3 One total per row, all starting at 0
- line 8 Each row adds into its own slot
- line 15-16 One column: c stays fixed and only r moves
?Wait, why doesn't rowTotals set each totals[r] to 0 first?
Neighbor questions look at the cells next to a cell: above, below, left and right. The danger is the edge. A cell in row 0 has nothing above it, so before you read g[r - 1][c], check that r > 0. Do the same for every direction.
1public static int neighborSum(int[][] g, int r, int c)2{3 int sum = 0;4 if (r > 0)15 {6 sum += g[r - 1][c];7 }8 if (r < g.length - 1)19 {10 sum += g[r + 1][c];11 }12 if (c > 0)213 {14 sum += g[r][c - 1];15 }16 if (c < g[0].length - 1)217 {18 sum += g[r][c + 1];19 }20 return sum;21}
- line 4-8 Is there a row above? a row below?
- line 12-16 Is there a column to the left? to the right?
Because each row is a reference, you can rearrange whole rows without touching their elements: the three-line swap works on rows too, with an int[] temporary.
I thought about it. Show me
?Wait, what does the ! do in !lot[r][c]?
1public static int firstFullRow(boolean[][] lot)2{3 for (int r = 0; r < lot.length; r++)4 {5 boolean full = true;16 for (int c = 0; c < lot[r].length; c++)7 {8 if (!lot[r][c])29 {10 full = false;211 }12 }13 if (full)314 {15 return r;316 }17 }18 return -1;419}
- line 5 Reset the flag at the start of every row
- line 8-10 One empty spot means this row is not full
- line 13-15 Decide after the whole row is checked
- line 18 Only after every row: no full row
- Rows on the outside, because the answer is a row: for (int r = 0; r < lot.length; r++).
- For each row, start by assuming it's full: boolean full = true; inside the outer loop.
Why?
Reset per row. If full were declared once before the loops, one empty space in row 0 would mark every later row as not full. - Check every space in the row: for (int c = 0; c < lot[r].length; c++) { if (!lot[r][c]) { full = false; } }
- After the inner loop, decide: if (full) { return r; }
Why?
The return must wait until the whole row is checked. - After both loops, return -1;. For { {true, false, true}, {true, true, true}, {false, true, true} } the method returns 1.
Answer: Loop over rows; set full = true per row; any false space sets full = false; after the row, return r if full; after all rows, return -1.
Stuck? Get a hint
- What kind of answer is it: a row, a column or a cell?
- Where should the "is this row full?" flag be reset?
- Where does return -1 go?
int[][] m = { {3, 8}, {7, 1}, {4, 6} };
int r0 = 0;
int c0 = 0;
for (int r = 0; r < m.length; r++)
{
for (int c = 0; c < m[0].length; c++)
{
if (m[r][c] > m[r0][c0])
{
r0 = r;
c0 = c;
}
}
}
System.out.println(r0 + " " + c0);- A0 1
- B1 0
- C8
- D2 1
Show the answer
A. The code tracks the position of the largest value. 8 is largest, at row 0, column 1, and nothing later is bigger, so it prints 0 1. The second choice is the trap: it swaps row and column. The third prints the value, but the code prints positions.

FRQ 4 is the 2D Array question, worth 6 points. Expect points for traversing the right rows and columns without going out of bounds, for the condition, for resetting per-row or per-column values in the right place, and for returning the right thing after the loops. Say each loop bound out loud: rows from g.length, columns from g[0].length.
- Per-row work: reset inside the outer loop. One column: fix c, move r. Whole grid: one variable outside both loops.
- Check the edges before reading a neighbor: r > 0, r < g.length - 1, c > 0, c < g[0].length - 1.
- Rows are references: you can swap or pass whole rows.
- Decide about a row after its inner loop ends, and return "not found" after all the loops.