Question: I need to make a method to find the col index of the minimum value in a specified row of a given a 2D array.Return
I need to make a method to find the col index of the minimum value in a specified row of a given a 2D array.Return -1 if an invalid row value is specified. You may test your code with the Test2DArr.java given in this unit.
Below is what I have, & it is not working correctly. I have entered 2 for the row, & it is returning col 0 as having the largest integer. It should be returning 3.
public class Practice {
public static void main(String[] args) {
int[][] arr2d = {{2,99,5,70},{2,3,5,99},{3,5,6,72}};
System.out.println("Array contents:");
display(arr2d);
int results = findColOfRowMin(arr2d, 2);
System.out.println("The index of the col with the max number is: " + results);
}
//*********************************************************************
public static void display(int[][] arr2d) {
for(int row=0; row for(int col=0; col System.out.printf("%4d", arr2d[row][col]); } // end col loop System.out.println(); } // end row loop } // end method //********************************************************************* public static int findColOfRowMin(int[][] arr2d, int row) { if(arr2d == null) { return -1; } else if(arr2d[0].length == 0 || row < 0 || arr2d.length <= row) { return -1; } else { int index = 0; for(int i=1; i if(arr2d[row][index] > arr2d[row][i]) { index = i; } } return index; } } // end findColOfRowMin } // end class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
