Matrix Maximum & Minimum Value ISC Computer Science 2025 Practical

Write a 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 both M and N must be greater than 2 and less than 20. Allow the user to input integers into this matrix.

Perform the following tasks on the matrix:
(a) Display the input matrix.
(b) Find the maximum and minimum value in the matrix and display them along with their position.
(c) Sort the elements of the matrix in descending order using standard sorting technique and rearrange them in the matrix.
(d) Output the rearranged matrix.

Test your program for the following data and some random data:

Example 1
INPUT:
M = 3
N = 4
ENTER ELEMENTS OF THE MATRIX:

 8 7 9  3
-2 0 4 5
1 3 6 -4

OUTPUT:
ORIGINAL MATRIX

 8 7 9  3
-2 0 4 5
1 3 6 -4

LARGEST NUMBER: 9
ROW = 0
COLUMN = 2
SMALLEST NUMBER = -4
ROW = 2
COLUMN = 3
REARRANGED MATRIX

-4 -2 0 1
3 3 4 5
6 7 8 9

Example 2
INPUT:
M = 3
N = 3
ENTER ELEMENTS OF THE MATRIX:

 7  9 3
-2 4 5
1 16 4

OUTPUT:
ORIGINAL MATRIX

 7  9 3
-2 4 5
1 16 4

LARGEST NUMBER: 16
ROW = 2
COLUMN = 1
SMALLEST NUMBER: -2
ROW = 1
COLUMN = 0
REARRANGED MATRIX

-2 1  3
4 4 5
7 9 16

Example 3
INPUT:
M = 3
N = 22
OUTPUT: SIZE OUT OF RANGE

import java.util.Scanner;
class MaxMin{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("M = ");
        int m = Integer.parseInt(in.nextLine());
        System.out.print("N = ");
        int n = Integer.parseInt(in.nextLine());
        if(m < 3 || n < 3 || m > 19 || n > 19){
            System.out.println("SIZE OUT OF RANGE");
            return;
        }
        int a[][] = new int[m][n];
        int b[] = new int[m * n];
        int index = 0;
        System.out.println("ENTER ELEMENTS OF THE MATRIX:");
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                a[i][j] = Integer.parseInt(in.nextLine());
                b[index++] = a[i][j];
            }
        }
        System.out.println("ORIGINAL MATRIX");
        display(a);
        int max = a[0][0];
        int i1 = 0;
        int j1 = 0;
        int min = a[0][0];
        int i2 = 0;
        int j2 = 0;
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(a[i][j] > max){
                    max = a[i][j];
                    i1 = i;
                    j1 = j;
                }
                if(a[i][j] < min){
                    min = a[i][j];
                    i2 = i;
                    j2 = j;
                }
            }
        }
        System.out.println("LARGEST NUMBER: " + max);
        System.out.println("ROW = " + i1);
        System.out.println("COLUMN = " + j1);
        System.out.println("SMALLEST NUMBER: " + min);
        System.out.println("ROW = " + i2);
        System.out.println("COLUMN = " + j2);
        for(int i = 0; i < b.length; i++){
            for(int j = 0; j < b.length - 1 - i; j++){
                if(b[j] > b[j + 1]){
                    int temp = b[j];
                    b[j] = b[j + 1];
                    b[j + 1] = temp;
                }
            }
        }
        index = 0;
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                a[i][j] = b[index++];
            }
        }
        System.out.println("REARRANGED MATRIX");
        display(a);
    }
    public static void display(int mat[][]){
        for(int i = 0; i < mat.length; i++){
            for(int j = 0; j < mat[0].length; j++){
                System.out.print(mat[i][j] + "\t");
            }
            System.out.println();
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *