How to find the exact location of a maximum and minimum number in a matrix. The code can display the max and min values in the matrix but I need to find the exact location such as row and columns. Needs to be formatted like this. The maximum number is 2 at row 0, column 3.
import java.util.Scanner;
public class alarconh_Program2 {
public static void main(String[] args){
Scanner input = new Scanner (System.in);
int row = 0;
int col = 0;
double col1sum,col2sum,col3sum,col4sum,col1avg,col2avg,col3avg,col4avg;
System.out.println(" (Col) x (Row) ");
System.out.println("Enter the number of rows");
row = input.nextInt();
System.out.println("Enter the number of columns");
col = input.nextInt();
double [][] matrix = new double[row][col];
for (int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
matrix [i][j] = input.nextDouble();
}
}
System.out.println();
String formathd="The Matrix is";
System.out.printf(formathd);
for (int i1 = 0; i1 < matrix.length; i1++){
System.out.println();
for(int j1 = 0; j1 < matrix[i1].length; j1++){
String Table=" %2.1f ";
System.out.printf(Table, matrix [i1][j1]);
}
}
col1sum = matrix[0][0] + matrix[1][0] + matrix[2][0];
col2sum = matrix[0][1] + matrix[1][1] + matrix[2][1];
col3sum = matrix[0][2] + matrix[1][2] + matrix[2][2];
col4sum = matrix[0][3] + matrix[1][3] + matrix[2][3];
System.out.println();
String SUM = "%2.1f %2.1f %2.1f %2.1f :SUM";
System.out.printf(SUM,col1sum,col2sum,col3sum,col4sum);
col1avg= col1sum/row;
col2avg = col2sum/row;
col3avg = col3sum/row;
col4avg = col4sum/row;
System.out.println();
String AVG = " %2.1f %2.1f %2.1f %2.1f :AVG";
System.out.printf(AVG,col1avg,col2avg,col3avg,col4avg);
System.out.println();
double maxValue = Integer.MIN_VALUE;
for (int i2 = 0; i2 < matrix.length; i2++)
for (int j2 = 0; j2 < matrix[i2].length; j2++)
if (matrix[i2][j2] > maxValue)
maxValue = matrix[i2][j2];
System.out.println("Maximum numbers is " +maxValue + " at row " +row+ ", and column " +col);
double minValue =Integer.MAX_VALUE;
for (int i2 = 0; i2 < matrix.length; i2++)
for (int j2 = 0; j2 < matrix[i2].length; j2++)
if (matrix[i2][j2] < minValue)
minValue = matrix[i2][j2];
System.out.println("Minimum numbers is " +minValue+ " at row " +row+ ", and column " +col);
}
}
if you want to print the exact location of minimum and maximum, edit your last loop to this :-
for (int i2 = 0; i2 < matrix.length; i2++)
for (int j2 = 0; j2 < matrix[i2].length; j2++)
if (matrix[i2][j2] < minValue)
{
minValue = matrix[i2][j2];
row = i2; // stores the current row to row variable
col = j2 // stores current columb to col variable
}
System.out.println("Minimum numbers is " +minValue+ " at row " +row+ ", and column " +col);
max=m[0][0];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(m[i][j]>max)
{
max=m[i][j];
row = i;
col = j;
}
}
}
System.out.println("Maximum element in matrix is "+max+" at m["+row+"]["+col+"]");
//MINIMUM element of the matrix
min=m[0][0];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(m[i][j]<min)
{
min=m[i][j];
row = i;
col = j;
}
}
}
System.out.println("Minimum elementin matrix is "+min+" at m["+row+"]["+col+"]");
For larger matrices, an O^2 approach is going to be cost prohibitive.
Could also maintain metadata as the matrix is populated. For an r(ow) xc(olumn) matrix, create an additional single-dimension array of size c and whenever a new max value is put into row n, update the additional array for index n with the c of the new largest value.
It's a trade-off between memory and processing, but for a non-trivial use case it's likely going to be faster.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.