Maximum and Minimum Value in Matrix ISC Computer Science 2012 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 ascending order using any standard sorting technique and rearrange them in the matrix.

(d) Output the rearranged matrix.

Test your program with the sample data and some random data:

Example 1
INPUT:
M = 3
N = 4
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 = 22
OUTPUT:
SIZE OUT OF RANGE

import java.util.Scanner;
class Matrix{
    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 matrix elements:");
        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");
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++)
                System.out.print(a[i][j] + "\t");
            System.out.println();
        }
        int max = a[0][0];
        int min = a[0][0];
        int rowIndex1 = 0;
        int colIndex1 = 0;
        int rowIndex2 = 0;
        int colIndex2 = 0;
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(max < a[i][j]){
                    max = a[i][j];
                    rowIndex1 = i;
                    colIndex1 = j;
                }
                if(min > a[i][j]){
                    min = a[i][j];
                    rowIndex2 = i;
                    colIndex2 = j;
                }
            }
        }
        System.out.println("LARGEST NUMBER: " + max);
        System.out.println("ROW: " + rowIndex1);
        System.out.println("COLUMN: " + colIndex1);
        System.out.println("SMALLEST NUMBER: " + min);
        System.out.println("ROW: " + rowIndex2);
        System.out.println("COLUMN: " + colIndex2);
        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");
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++)
                System.out.print(a[i][j] + "\t");
            System.out.println();
        }
    }
}

Comments

Popular posts from this blog

Encrypt Program ISC Specimen 2023 Theory

No Repeat Program ISC Computer Science 2022 Semester 2 Theory

Bank Inheritance Program ISC Specimen 2023 Theory