generated from nighthawkcoders/student_2025
-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
2021 FRQ:
Part A.
public static boolean isNonZeroRow(int[][] array2D, int r) {
for (int value : array2D[r]) {
if (value == 0) {
return false;
}
}
return true;
}
Loops through all elements in the specified row r.
If any element is 0, the method returns false.
If the loop completes without finding a 0, the method returns true.
Part B.
public static int[][] resize(int[][] array2D) {
int numRows = numNonZeroRows(array2D); // Get the number of non-zero rows
int[][] result = new int[numRows][]; // Create a new 2D array with the correct size
int index = 0;
for (int i = 0; i < array2D.length; i++) {
if (isNonZeroRow(array2D, i)) {
result[index] = array2D[i]; // Add the non-zero row to the result
index++;
}
}
return result;
}
First determines how many rows contain only non-zero elements using numNonZeroRows.
Initializes a new 2D array (result) with the appropriate number of rows.
Iterates through each row of the input array. For every row that satisfies isNonZeroRow, it adds that row to the result array.
Explanation of errors:
- The method numNonZeroRows was not defined in the code until I added it later
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels