Angie A.
asked 05/04/20Need some help with ragged array method I was working on I need some help with explaining method
getHighestInColumnIndex
public static int getHighestInColumnIndex(double[][] data,
int col)
Returns index of the largest element of the selected column in the two dimensional array index 0 refers to the first column. If a
row in the two dimensional array doesn't have this column index, it is not an error, it doesn't participate in this method.
Parameters:
data - the two dimensional array
col - the column index to find the largest element of (0 refers to the first column)
Returns:
the index of the largest element of the column
my code
ublic static int getHighestInColumnIndex(double[][] data, int col) {
double max = data[0][col];
int location = -1;
for(int i =0; i<data.length; i++) {
if(col>= 0 && col<data[i].length)
if(data[i][col]>max) {
location = i;
}
}
return location;
}
1 Expert Answer

Patrick B. answered 05/04/20
Math and computer tutor/teacher
You're on the right track, but in the for loop, you have to compare the variable col against the size of data[i].length, or else you are going to compare it against something NULL or undefined, and then it crashes!!!
Here ya go:
class AngieARaggedArrays
{
public static final int MAX_COLUMNS=6;
/*********************************************
Returns the largest column value or -1
************************************************/
public static double getHighestInColumn(double[][]data, int col)
{
double maxVal=-1;
for (int rowLoop=0; rowLoop<data.length; rowLoop++)
{
if (
(col>=0) && (col<data[rowLoop].length)
)
// Then that column EXISTS in this particular row
{
if ( data[rowLoop][col]>maxVal) //the current max value has been DEFEATED!!
{ // new champion ;-)
maxVal = data[rowLoop][col];
}
}
}
return(maxVal);
}
public static void main(String args[])
{
double A[][] = { {5, 4, 3, 2, 1},
{4, 3, 2},
{1, 4, 7, 9, 3, 5},
{7, 8, 4, 1, 2},
{9, 3},
{1, 3, 3, 4, 5},
{2},
{7, 6, 5, 4}
};
AngieARaggedArrays x = new AngieARaggedArrays();
for (int columnLoop=0; columnLoop<x.MAX_COLUMNS; columnLoop++)
{
System.out.println(" The highest value in column " + columnLoop
+ " is : " +x.getHighestInColumn(A,columnLoop));
}
}
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Angie A.
I keep get assertion error05/04/20