2D Arrays Part 2 HW
here is my 2d arrays part 2 HW frq
Part A
public void repopulate() {
// iterates through each row of the grid
for (int row = 0; row < grid.length; row++) {
// iterate through each column of the grid
for (int col = 0; col < grid[row].length; col++) {
int value;
// generates random values until the criteria are met
do {
value = (int)(Math.random() * MAX) + 1; // generates a random number between 1 and MAX (inclusive)
} while (value % 10 != 0 || value % 100 == 0); // ensures the value is divisible by 10 but not by 100
grid[row][col] = value; // assigns the valid value to the grid
}
}
}
Part B
public int countIncreasingCols() {
int count = 0; // counter for the number of increasing columns
// iterate through each column of the grid
for (int col = 0; col < grid[0].length; col++) {
boolean isIncreasing = true; // assume the column is increasing
// check each row in the current column (starting from the second row)
for (int row = 1; row < grid.length; row++) {
if (grid[row][col] < grid[row - 1][col]) { // if current value is less than the previous value
isIncreasing = false; // columns is not increasing
break; // exits the loop early as we found a violation
}
}
if (isIncreasing) {
count++; // incrementss count if the column is increasing
}
}
return count; // returns the total number of increasing columns
}