Question: public static void findExtrema ( int [ ] [ ] array ) { int rows = array.length; int cols = array.length; int [ ] maxRows

public static void findExtrema(int[][] array){ int rows = array.length; int cols = array.length; int[] maxRows = new int[rows]; int[] minRows = new int[rows]; int[] maxCols = new int[cols]; int[] minCols = new int[cols]; // Initialize minCols and maxCols with the first row values for (int j =0; j < cols; j++){ maxCols[j]= array[j]; minCols[j]= array[j]; }// Find max and min for each row for (int i =0; i < rows; i++){ int maxRow = array[i]; int minRow = array[i]; for (int j =0; j < cols; j++){ if (array[i][j]> maxRow){ maxRow = array[i][j]; } if (array[i][j]< minRow){ minRow = array[i][j]; }// Update max and min for each column if (array[i][j]> maxCols[j]){ maxCols[j]= array[i][j]; } if (array[i][j]< minCols[j]){ minCols[j]= array[i][j]; }} maxRows[i]= maxRow; minRows[i]= minRow; }// Print results System.out.println("The maximum values along the rows are "+ java.util.Arrays.toString(maxRows)+"."); System.out.println("The minimum values along the rows are "+ java.util.Arrays.toString(minRows)+"."); System.out.println("The maximum values along the columns are "+ java.util.Arrays.toString(maxCols)+"."); System.out.println("The minimum values along the columns are "+ java.util.Arrays.toString(minCols)+"."); } public static void main(String[] args){ int[][] array ={{1,2,3},{4,5,6},{7,8,9}}; findExtrema(array); }}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!