Which algorithm correctly totals every value in int[][] data?
total at the start of each row.data[row][0].total after the inner loop's first iteration.Explanation
Whole-array work uses two loops, one over a.length and one over a[0].length. A sum cannot be reported until every required cell has been processed, so the return belongs after the traversal.
- A is not correct: Resetting
totalat the start of each row throws away everything added in the earlier rows, leaving only the last row counted. - B is not correct: Adding only
data[row][0]touches the first column of each row and never reaches the remaining columns of the array. - D is not correct: Returning inside the inner loop after one iteration is an early exit, and an accumulator has no correct value until all cells are added.