Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2D Arrays Pt 2 #15

Open
SGTech08 opened this issue Jan 24, 2025 · 0 comments
Open

2D Arrays Pt 2 #15

SGTech08 opened this issue Jan 24, 2025 · 0 comments

Comments

@SGTech08
Copy link
Owner

a)

public void repopulate() {
// iterate through each row of the grid
for (int row = 0; row < grid.length; row++) {
// iterate through each column of the current row
for (int col = 0; col < grid[row].length; col++) {
// initialize a random value that meets the criteria
int value;
do {
// generate a random value between 1 and MAX (inclusive)
value = (int) (Math.random() * MAX) + 1;
} while (value % 10 != 0 || value % 100 == 0); // ensure value is divisible by 10 but not 100

        // assign the valid value to the current grid element
        grid[row][col] = value;
    }
}

}

b)

public int countAscendingCols() {
// initialize a counter to track columns in ascending order
int ascendingCount = 0;

// iterate through each column in the grid
for (int col = 0; col < grid[0].length; col++) {
    boolean isAscending = true;

    // check if the current column is in ascending order
    for (int row = 1; row < grid.length; row++) {
        if (grid[row][col] < grid[row - 1][col]) {
            isAscending = false; // set to false if a descending value is found
        }
    }

    // increment the counter if the column is in ascending order
    if (isAscending) {
        ascendingCount++;
    }
}

// return the total number of ascending columns
return ascendingCount;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant