Skip to the content.
Example Questions Strategies Homework

Array/Homework

AP CSA FRQ Array/Arraylist Homework

AP CSA

Homework

Write a program that randomly fills in 0s and 1s into an n-by-n matrix, prints the matrix, and finds the rows and columns with the most 1s. (Hint: Use two ArrayLists to store the row and column indices with the most 1s.)

Here is a sample run of the program, printed in the console:

Enter the array size n: 4
The random array is
0011
0011
1101
1010
The largest row index: 2
The largest column index: 2, 3 
import java.util.ArrayList;

public class BinaryGridAnalyzer {
    public static int[][] generateRandomGrid(int gridSize) {
        int[][] binaryGrid = new int[gridSize][gridSize];
        for (int rowIndex = 0; rowIndex < gridSize; rowIndex++) {
            for (int colIndex = 0; colIndex < gridSize; colIndex++) {
                binaryGrid[rowIndex][colIndex] = (int) (Math.random() * 2);
            }
        }
        return binaryGrid;
    }

    public static void printGrid(int[][] binaryGrid) {
        for (int[] row : binaryGrid) {
            for (int cellValue : row) {
                System.out.print(cellValue);
            }
            System.out.println();
        }
    }

    public static ArrayList<Integer> findMaxRows(int[][] binaryGrid) {
        ArrayList<Integer> rowIndicesWithMaxOnes = new ArrayList<>();
        int maxOnesInRow = 0;

        for (int rowIndex = 0; rowIndex < binaryGrid.length; rowIndex++) {
            int oneCountInRow = 0;
            for (int cellValue : binaryGrid[rowIndex]) {
                if (cellValue == 1) oneCountInRow++;
            }
            if (oneCountInRow > maxOnesInRow) {
                maxOnesInRow = oneCountInRow;
                rowIndicesWithMaxOnes.clear();
                rowIndicesWithMaxOnes.add(rowIndex);
            } else if (oneCountInRow == maxOnesInRow) {
                rowIndicesWithMaxOnes.add(rowIndex);
            }
        }

        return rowIndicesWithMaxOnes;
    }

    public static ArrayList<Integer> findMaxColumns(int[][] binaryGrid) {
        ArrayList<Integer> colIndicesWithMaxOnes = new ArrayList<>();
        int maxOnesInColumn = 0;

        for (int colIndex = 0; colIndex < binaryGrid[0].length; colIndex++) {
            int oneCountInColumn = 0;
            for (int rowIndex = 0; rowIndex < binaryGrid.length; rowIndex++) {
                if (binaryGrid[rowIndex][colIndex] == 1) oneCountInColumn++;
            }
            if (oneCountInColumn > maxOnesInColumn) {
                maxOnesInColumn = oneCountInColumn;
                colIndicesWithMaxOnes.clear();
                colIndicesWithMaxOnes.add(colIndex);
            } else if (oneCountInColumn == maxOnesInColumn) {
                colIndicesWithMaxOnes.add(colIndex);
            }
        }

        return colIndicesWithMaxOnes;
    }

    public static void main(String[] args) {
        int gridSize = 5;
        int[][] binaryGrid = generateRandomGrid(gridSize);

        System.out.println("Grid:");
        printGrid(binaryGrid);

        System.out.println("\nrows w/ maximum 1s: " + findMaxRows(binaryGrid));
        System.out.println("columns w/ maximum 1s: " + findMaxColumns(binaryGrid));
    }
}

BinaryGridAnalyzer.main(new String[4]);
Generated Binary Grid:
10000
10011
00101
11000
11110

Rows with maximum 1s: [4]
Columns with maximum 1s: [0]