Skip to the content.

2D Arrays FRQ Homework

public class ArrayResizer {

    /** Returns true if and only if every value in row r of array2D is non-zero.
     * Precondition: r is a valid row index in array2D.
     * Postcondition: array2D is unchanged.
     */
    public static boolean isNonZeroRow(int[][] array2D, int r) {
        /* to be implemented in Part A */
    }
    /** Returns the number of rows in array2D that contain all non-zero values.
     * Postcondition: array2D is unchanged.
     */
    public static int numNonZeroRows(int[][] array2D) {
        /* implementation not shown */
    }
    /** Returns a new, possibly smaller, two-dimensional array that contains only rows
     * from array2D with no zeros, as described in part (b).
     * Precondition: array2D contains at least one column and at least one row with no zeros.
     * Postcondition: array2D is unchanged.
     */
    public static int[][] resize(int[][] array2D) {
        /* to be implemented in Part B */
    }
}

My solution

public class ArrayResizer {

    /** Returns true if and only if every value in row r of array2D is non-zero.
     * Precondition: r is a valid row index in array2D.
     * Postcondition: array2D is unchanged.
     */
    public static boolean isNonZeroRow(int[][] array2D, int r) {
        for (int c = 0; c < array2D[r].length; c++) {
            if (array2D[r][c] == 0) {
                return false;
            }
        }
        return true;
    }
    /** Returns the number of rows in array2D that contain all non-zero values.
     * Postcondition: array2D is unchanged.
     */
    public static int numNonZeroRows(int[][] array2D) {
        /* implementation not shown */
    }
    /** Returns a new, possibly smaller, two-dimensional array that contains only rows
     * from array2D with no zeros, as described in part (b).
     * Precondition: array2D contains at least one column and at least one row with no zeros.
     * Postcondition: array2D is unchanged.
     */
    public static int[][] resize(int[][] array2D) {
        int rows = array2D.length;
        int cols = array2D[0].length;
        int[][] result = new int[numNonZeroRows(array2D)][cols];
        int nextRow = 0;
        for (int r = 0; r < rows; r++) {
            if (isNonZeroRow(array2D, r)) {
                for (int c = 0; c < cols; c++) {
                    result[nextRow][c] = array2D[r][c];
                }
                nextRow++;
            }
        }
        return result;
    }

Extra Hack

import java.util.Scanner;

public class ArrayResizer {

    /** 
     * Returns true if and only if every value in row r of array2D is non-zero.
     * Precondition: r is a valid row index in array2D.
     * Postcondition: array2D is unchanged.
     */
    public static boolean isNonZeroRow(int[][] array2D, int r) {
        for (int c = 0; c < array2D[r].length; c++) {
            if (array2D[r][c] == 0) {
                return false;
            }
        }
        return true;
    }

    /** 
     * Returns the number of rows in array2D that contain all non-zero values.
     * Postcondition: array2D is unchanged.
     */
    public static int numNonZeroRows(int[][] array2D) {
        int count = 0;
        for (int r = 0; r < array2D.length; r++) {
            if (isNonZeroRow(array2D, r)) {
                count++;
            }
        }
        return count;
    }

    /** 
     * Returns a new, possibly smaller, two-dimensional array that contains only rows
     * from array2D with no zeros, as described in part (b).
     * Precondition: array2D contains at least one column and at least one row with no zeros.
     * Postcondition: array2D is unchanged.
     */
    public static int[][] resize(int[][] array2D) {
        int rows = array2D.length;
        int cols = array2D[0].length;
        int[][] result = new int[numNonZeroRows(array2D)][cols];
        int nextRow = 0;
        for (int r = 0; r < rows; r++) {
            if (isNonZeroRow(array2D, r)) {
                for (int c = 0; c < cols; c++) {
                    result[nextRow][c] = array2D[r][c];
                }
                nextRow++;
            }
        }
        return result;
    }

    /** 
     * Extra Game: Row Checker Game.
     * Allows the user to guess if a row is non-zero.
     */
    public static void playRowCheckerGame(int[][] array2D) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Welcome to the Row Checker Game!");
        System.out.println("Try to guess if a row is non-zero or not.");

        while (true) {
            System.out.println("\nEnter a row index between 0 and " + (array2D.length - 1) + " (or -1 to quit):");
            int rowIndex = scanner.nextInt();

            if (rowIndex == -1) {
                System.out.println("Thanks for playing! Goodbye!");
                break;
            }

            if (rowIndex < 0 || rowIndex >= array2D.length) {
                System.out.println("Invalid row index! Please try again.");
                continue;
            }

            System.out.println("Do you think this row is non-zero? (yes/no):");
            String userGuess = scanner.next();

            boolean isNonZero = isNonZeroRow(array2D, rowIndex);
            boolean guessedCorrectly = (isNonZero && userGuess.equalsIgnoreCase("yes")) || (!isNonZero && userGuess.equalsIgnoreCase("no"));

            if (guessedCorrectly) {
                System.out.println("Correct! Row " + rowIndex + " is " + (isNonZero ? "non-zero." : "not non-zero."));
            } else {
                System.out.println("Wrong! Row " + rowIndex + " is " + (isNonZero ? "non-zero." : "not non-zero."));
            }
        }

        scanner.close();
    }

    public static void main(String[] args) {
        // Example 2D array for testing
        int[][] array2D = {
            {1, 2, 3},
            {4, 0, 6},
            {7, 8, 9}
        };

        // Play the extra game
        playRowCheckerGame(array2D);
    }
}
ArrayResizer.main(null);
Welcome to the Row Checker Game!
Try to guess if a row is non-zero or not.

Enter a row index between 0 and 2 (or -1 to quit):
Do you think this row is non-zero? (yes/no):
Correct! Row 1 is not non-zero.

Enter a row index between 0 and 2 (or -1 to quit):
Do you think this row is non-zero? (yes/no):
Correct! Row 0 is non-zero.

Enter a row index between 0 and 2 (or -1 to quit):
Do you think this row is non-zero? (yes/no):
Wrong! Row 2 is non-zero.

Enter a row index between 0 and 2 (or -1 to quit):
Thanks for playing! Goodbye!