Highest Row-wise and Lowest Column-wise Elements in a Matrix in Java
Write a Java program to declare a matrix a[][] of order m × n, where 'm' is the number of rows and 'n' is the number of columns such that m and n are positive integers. Accept the elements i.e. positive integers into this matrix.
Now check and display the highest elements row-wise and the lowest elements column-wise. Test the program for the following data and some random data.
import java.util.Scanner;
class Matrix{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.print("m = ");
int m = Math.abs(Integer.parseInt(in.nextLine()));
System.out.print("n = ");
int n = Math.abs(Integer.parseInt(in.nextLine()));
int a[][] = new int[m][n];
int i, j;
System.out.println("Enter matrix elements:");
for(i = 0; i < m; i++){
for(j = 0; j < n; j++){
a[i][j] = Math.abs(Integer.parseInt(in.nextLine()));
}
}
int high = a[0][0];
int small = a[0][0];
for(i = 0; i < m; i++){
high = a[i][0];
for(j = 0; j < n; j++){
if(a[i][j] > high){
high = a[i][j];
}
System.out.print(a[i][j] + "\t");
}
System.out.println("="+high);
}
for(j = 0; j < n; j++){
small = a[0][j];
for(i = 0; i < m; i++){
if(a[i][j] < small){
small = a[i][j];
}
}
System.out.print("="+small + "\t");
}
}
}
class Matrix{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.print("m = ");
int m = Math.abs(Integer.parseInt(in.nextLine()));
System.out.print("n = ");
int n = Math.abs(Integer.parseInt(in.nextLine()));
int a[][] = new int[m][n];
int i, j;
System.out.println("Enter matrix elements:");
for(i = 0; i < m; i++){
for(j = 0; j < n; j++){
a[i][j] = Math.abs(Integer.parseInt(in.nextLine()));
}
}
int high = a[0][0];
int small = a[0][0];
for(i = 0; i < m; i++){
high = a[i][0];
for(j = 0; j < n; j++){
if(a[i][j] > high){
high = a[i][j];
}
System.out.print(a[i][j] + "\t");
}
System.out.println("="+high);
}
for(j = 0; j < n; j++){
small = a[0][j];
for(i = 0; i < m; i++){
if(a[i][j] < small){
small = a[i][j];
}
}
System.out.print("="+small + "\t");
}
}
}
Comments
Post a Comment