In this post of Java program to find maximum and minimum numbers in the given matrix.
We will learn how to write a java program to find the maximum and minimum number in the given matrix (2-Dimensional Array).
Logic here what we used to have two variables for maximum and minimum numbers, initially assign the element at the first index of the matrix to both the variables.
After that 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.
If we find max number is greater than the column element then check if minimum number is greater than the column element, if yes then assign column element to the minimum number.
You may also like –
Find largest and smallest number in the given Array
Find duplicate elements in an Array
Java program to Remove Duplicate Elements From an Array
Java program to add two matrices
Matrix Subtraction Java program
Java program to find maximum and minimum numbers in the given matrix
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
package com.kkjavatutorials.client; import java.util.Scanner; /** * @author KK JavaTutorials * Java program to find maximum and minimum numbers in the given matrix */ public class MatrixMinMaxElementsTest { public static void main(String[] args) { int numberOfRows; int numberOfColumns; Scanner scanner = null; int[][] inputMatrixArray = null; try { scanner = new Scanner(System.in); System.out.println("Enter number of rows in matrix:"); numberOfRows = scanner.nextInt(); System.out.println("Enter number of columns in matrix:"); numberOfColumns = scanner.nextInt(); inputMatrixArray = new int[numberOfRows][numberOfColumns]; System.out.println("Enter matrix Elements:"); for (int i = 0; i < numberOfRows; i++) { System.out.println("Enter numbers for row-" + (i + 1)); for (int j = 0; j < numberOfColumns; j++) { inputMatrixArray[i][j] = scanner.nextInt(); } } } catch (Exception e) { e.printStackTrace(); }finally { if(scanner != null) scanner.close(); } //Displaying the entered matrix System.out.println("Matrix you have entered::"); for (int i = 0; i < inputMatrixArray.length; i++) { System.out.println(); for (int j = 0; j < inputMatrixArray[i].length; j++) { System.out.print(inputMatrixArray[i][j] + " "); } } System.out.println(); findMinMaxElementsInMatrix(inputMatrixArray); } //Method to find max and min elements from the input matrix private static void findMinMaxElementsInMatrix(int[][] inputMatrixArray) { //Initially assigning the first matrix element to both the variables int maxNumber = inputMatrixArray[0][0]; int minNumber = inputMatrixArray[0][0]; for (int i = 0; i < inputMatrixArray.length; i++) { for (int j = 0; j < inputMatrixArray[i].length; j++) { if (maxNumber < inputMatrixArray[i][j]) { maxNumber = inputMatrixArray[i][j]; } else if (minNumber > inputMatrixArray[i][j]) { minNumber = inputMatrixArray[i][j]; } } } System.out.println("Largest number: " + maxNumber + " Smallest number: " + minNumber); } } |
Program output for 3X4 Matrix:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
Enter number of rows in matrix: 3 Enter number of columns in matrix: 4 Enter matrix Elements: Enter numbers for row-1 23 12 34 45 Enter numbers for row-2 90 23 56 67 Enter numbers for row-3 78 90 345 45 Matrix you have entered:: 23 12 34 45 90 23 56 67 78 90 345 45 Largest number: 345 Smallest number: 12 |
That’s all for this topic Java program to find maximum and minimum numbers in the given matrix. If you have any doubts or any suggestions to make please drop a comment.