Question
I need help solving this practice problem: I need to make a method to find the col index of the minimum value in a specified
I need help solving this practice problem:
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.
Here is what I have, but I'm getting an ArrayIndexOutOfBoundsException on the for loop on the findRowOfColMax method. I'm missing something, & I cannot see it.
public class Practice {
public static void main(String[] args) {
int[][] arr2d = {{2,3,5,70},{2,3,5,99},{3,5,6,72}};
System.out.println("Array contents:");
display(arr2d);
int results = findRowOfColMax(arr2d, arr2d.length);
System.out.println("The index of the row 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 findRowOfColMax(int[][] arr2d, int col) { if(arr2d.length == 0 || col < 0 || arr2d[0].length < col) { return -1; } else { int index = 0; for(int i=1; i if(arr2d[index][col] < arr2d[i][col]) { index = i; } } return index; } } // end findRowOfColMax }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started