Part A
public void repopulate() {
for (int rowNumber = 0; rowNumber < grid.length; rowNumber++) {
for (int colNumber = 0; colNumber < grid[0].length; colNumber++) {
int value = (int) (Math.random() * MAX) + 1;
while (value % 10 != 0 || value % 100 == 0) {
value = (int) (Math.random() * MAX) + 1;
}
grid[rowNumber][colNumber] = value;
}
}
}
Part A Grading
Traverses grid (no bounds errors)
- Does it access all elements of the grid?
- Yes, the code correctly traverses all rows (rowNumber) and columns (colNumber) of the grid using standard for-loops.
- There are no bounds errors because the loops use < grid.length and < grid[0].length, which correctly iterate within bounds.
- The loops also properly maintain indices, and grid[rowNumber][colNumber] is used to access and modify elements directly.
Score: 1/1
Generates a random integer in a range based on MAX
- Does it generate a random integer in the correct range?
- Yes, the code uses
(int)(Math.random() * MAX) + 1
, which generates a random integer between 1 and MAX (inclusive). - The + 1 ensures that 1 is included in the range, and the cast to (int) is correctly applied.
Score: 1/1
Ensures all produced values are divisible by 10 but not by 100
- Does it ensure valid values?
- The while loop ensures that
value % 10 == 0
(divisible by 10) andvalue % 100 != 0
(not divisible by 100). - The condition
value % 10 != 0 || value % 100 == 0
correctly identifies and rejects invalid values. - This guarantees that all produced values meet the criteria.
Score: 1/1
Assigns appropriate values to all elements of grid (algorithm)
- Does it assign valid values to all elements of the grid?
- Yes, all elements of the grid are assigned a valid value that satisfies the divisibility requirements.
- The algorithm ensures values are equally distributed within the specified range, and no values outside the range are generated.
- The code does not exclude any valid values due to logic errors.
Score: 1/1
Final Score: 4/4
Part B
public int countIncreasingCols(int[][] array) {
int count = 0; // Initialize a counter to keep track of strictly increasing columns
// Loop through each column of the array
for (int j = 0; j < array[0].length; j++) {
boolean isIncreasing = true; // Assume the current column is strictly increasing
// Loop through each row in the current column to check if it's strictly increasing
for (int i = 0; i < array.length - 1; i++) {
// Compare the current row element with the element in the next row
if (array[i][j] >= array[i + 1][j]) {
isIncreasing = false; // If the current element is greater than or equal to the next, the column is not strictly increasing
break; // Exit the inner loop early since the column is no longer valid
}
}
// If the column was strictly increasing, increment the count
if (isIncreasing) {
count++;
}
}
// Return the total count of strictly increasing columns
return count;
}
Part B Grading
Traverses grid in column-major order (1 point)
- The outer loop
(for (int j = 0; j < array[0].length; j++))
iterates through columns, and the inner loop(for (int i = 0; i < array.length - 1; i++))
iterates through rows within each column. The traversal follows column-major order.
Score: 1/1
Compares two elements in the same column (1 point)
- Inside the inner loop, the code compares array
[i][j]
and array[i + 1][j]
, which are elements in the same column (j).
Score: 1/1
Determines whether a single column is in increasing order (1 point)
- The variable isIncreasing is initialized to true before checking a column.
- It is set to false if any pair of adjacent elements in the column is not in increasing order
(array[i][j] >= array[i + 1][j])
, and the loop exits early. - Edge Cases: The code does not access out-of-bounds indices due to properly set loop bounds.
Score: 1/1
Counts all columns that are identified as increasing (1 point)
- Code Analysis: The code checks each column, and if isIncreasing remains true, it increments the count variable
(count++)
.
Score: 1/1
Returns the calculated count of increasing columns (1 point)
- Code Analysis: The method correctly returns the value of count after traversing all columns.
Score: 1/1
Final Score: 5/5 Points