8.10. More Code Practice with 2D Arrays

Replace the “ADD CODE HERE” below to finish a function String findAndReplaceFirst(String[][] sentence, String target, String replacement) that takes a 2D array of strings, a target string to search for, and a replacement string. The function searches the 2D array for the first occurrence of the target string and replaces it with the replacement string.

Replace the “ADD CODE HERE” below to finish a function findMaxOnes(matrix) that accepts a two-dimensional integer array matrix as input. The function should determine the row with the maximum number of occurrences of the integer 1 and return its index. If multiple rows have the same maximum number of 1s, return the index of the first such row encountered. For example: if the input matrix is: {[ 0, 1, 1 ], [ 1, 1, 0 ], [ 1, 0, 1 ]}. The function should return 1, as the second row contains the maximum number of 1s (2 in total).

You are given a 2D array representing the seating arrangement in a theater. Each cell in the array contains either 0 (indicating an available seat) or 1 (indicating an occupied seat). Additionally, each row represents a different row in the theater, and each column represents a different seat in that row. Your task is to determine whether a specific seat at a given row and column is available while ensuring that social distancing guidelines are followed. The social distancing guideline specifies that no two occupied seats should be adjacent horizontally, vertically, or diagonally. Thus, a seat is considered available only if all adjacent seats (including diagonals) are empty. Replace the “ADD CODE HERE” below to finish a function isSeatAvailable(int[][] theater, int row, int column) that takes the theater seating arrangement and the row and column indices of the seat as input and returns true if the seat is available while adhering to social distancing guidelines, and false otherwise.

Replace the “ADD CODE HERE” to finish a function diagonalSum(int[][] matrix) that takes the matrix as input and returns an array containing the sums of elements along both diagonals. The first element of the array should represent the sum of elements along the main diagonal, and the second element should represent the sum of elements along the opposite diagonal. In a square matrix, the main diagonal refers to the diagonal line of elements from the top left to the bottom right. Conversely, the opposite diagonal runs from the top right to the bottom left corner of the matrix. For example if the input matrix is { {1, 1, 1},{2, 2, 2},{1, 1, 2} }. The function should return [5,4]

You have attempted of activities on this page