Tuesday, December 21, 2021

Find The Maximum Element in Each Row of a Matrix Java Program

In this post we’ll see a Java program to find the maximum element in each row of a matrix.

For example if the matrix is as follows-

3  7  9 
12 89 23 
1  17 32

Then the output should be-

Maximum element in row 1- 9
Maximum element in row 2- 89
Maximum element in row 3- 32

Java program to find maximum element in each row of a matrix

Initially user will be prompted to enter the matrix elements to create a matrix. Also create an array having the same length as the number of rows in the matrix.

Then iterate the matrix one row at a time and compare each column element with the max number if max number is less than the column element then assign column element to the max number. After the row is iterated (outer loop finishes one iteration) assign the maximum number to the corresponding index of the created array.

public class MatrixMax {
  public static void main(String[] args) {
    //create matrix by taking input from user
    int rows; 
    int columns;
    Scanner scanner = new Scanner(System.in);
    // 
    System.out.println("Enter number of rows: ");
    rows = scanner.nextInt(); 
    
    System.out.println("Enter number of columns: "); 
    columns = scanner.nextInt(); 
    
    int[][] matrix = new int [rows][columns];
      
    System.out.println("Enter matrix numbers: "); 
    for (int i = 0; i < rows; i++) {
      System.out.println("Enter numbers for row - " + (i+1) + " and press enter"); 
      for (int j = 0; j < columns; j++) {
        matrix[i][j] = scanner.nextInt();
      }
    }
    scanner.close();
    // Displaying entered matrix
    System.out.println("Matrix as entered");
    for (int i = 0; i < matrix .length; i++) {
      System.out.println();
      for (int j = 0; j < matrix[i].length; j++) {
        System.out.print(matrix[i][j] + " ");
      }
    }
    System.out.println();
    // call method to find max element per row
    findMaxEachRow(matrix);
  }

  private static void findMaxEachRow(int[][] matrix){ 
    int[] result = new int[matrix.length];
    for (int i = 0; i < matrix.length; i++) {
      // Assign first element of the row as 
      // maximum in first iteration
      int maxNum = matrix[i][0];
      for (int j = 0; j < matrix[i].length; j++) {
        if(maxNum < matrix[i][j]){
          maxNum = matrix[i][j];
        }
        result[i] = maxNum;
      }     
    }
      
    // Display results
    for (int i = 0; i < result.length; i++) {
      System.out.println("Maximum element in row " + (i + 1) + "- " + result[i]);
    }
  }
}

Output

Enter number of rows: 
3
Enter number of columns: 
3
Enter matrix numbers: 
Enter numbers for row - 1 and press enter
12 20 67
Enter numbers for row - 2 and press enter
56 34 55
Enter numbers for row - 3 and press enter
1 2 78
Matrix as entered

12 20 67 
56 34 55 
1  2  78 
Maximum element in row 1- 67
Maximum element in row 2- 56
Maximum element in row 3- 78

If you are asked to find the minimum element in each row of a matrix then you need to change just this line-

if(maxNum < matrix[i][j]) to if(minNum > matrix[i][j])

Variable maxNum is changed to minNum for readability.

That's all for this topic Find The Maximum Element in Each Row of a Matrix Java Program. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page


Related Topics

  1. Matrix Subtraction Java Program
  2. Find Largest And Smallest Number in a Given Array Java Program
  3. How to Display Pyramid Patterns in Java - Part1
  4. Java Program to Display Prime Numbers
  5. Reverse Each Word in a String Java Program

You may also like-

  1. How to Convert Date to String in Java
  2. Invoking Getters And Setters Using Reflection in Java
  3. Producer-Consumer Java Program Using volatile
  4. How to Read Excel File in Java Using Apache POI
  5. Java Multithreading Interview Questions And Answers
  6. Is String Thread Safe in Java
  7. Java Object Cloning - clone() Method
  8. Dependency Injection in Spring Framework