Code with Vinay

ISC practical papers › ISC 2012 › Question 3

Sorting/Max/Min a 2D array

ISC 2012 Computer Science Practical, Question 3 · Arrays and matrices

The question

Write a program to declare a matrix A [ ] [ ] of order (MXN) 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

Question: ISC 2012 Computer Science Paper 2 (Practical), Question 3, set by the Council for the Indian School Certificate Examinations (CISCE). Solution and video: Vinay Singh.

Java solution

ISC2012Q3.java Download GitHub
import java.util.*;

class Matrix {
    private int A[][], M, N, minRow, minCol, maxRow, maxCol, min, max;

    public Matrix(int M, int N) {
        A = new int[M][N];
        this.M = M;
        this.N = N;
    }

    public void getInput() {
        Scanner in = new Scanner(System.in);
        for (int row = 0; row < M; row++) {
            for (int col = 0; col < N; col++) {
                A[row][col] = in.nextInt();
            }
        }
        in.close();
    }

    public void computeMinimumPosition(int startRow, int startCol) {
        min = A[startRow][startCol];
        minRow = startRow;
        minCol = startCol;
        for (int row = startRow; row < M; row++) {
            // startRow=0;
            for (int col = startCol; col < N; col++) {
                startCol = 0;
                if (A[row][col] < min) {
                    min = A[row][col];
                    minRow = row;
                    minCol = col;
                }
            }
        }
    }

    public void computeMaxiumPosition(int startRow, int startCol) {
        max = A[startRow][startCol];
        maxRow = startRow;
        maxCol = startCol;
        for (int row = startRow; row < M; row++) {
            // startRow=0;
            for (int col = startCol; col < N; col++) {
                startCol = 0;
                if (A[row][col] > max) {
                    max = A[row][col];
                    maxRow = row;
                    maxCol = col;
                }
            }
        }
    }

    public String toString() {
        String output = "";
        for (int row = 0; row < M; row++) {
            output += "\t";
            for (int col = 0; col < N; col++) {
                output += A[row][col];
                if (col < N - 1)
                    output += "\t";
            }
            output += "\n";
        }
        return output;
    }

    public void showMaxDetails() {
        computeMaxiumPosition(0, 0);
        System.out.println("LARGEST NUMBER: " + max);
        System.out.println("\tROW = " + maxRow);
        System.out.println("\tCOLUMN = " + maxCol);
    }

    public void showMinDetails() {
        computeMinimumPosition(0, 0);
        System.out.println("SMALLEST NUMBER: " + min);
        System.out.println("\tROW = " + minRow);
        System.out.println("\tCOLUMN = " + minCol);
    }

    public void sortAscending() {
        int temp;
        for (int row = 0; row < M; row++) {
            for (int col = 0; col < N; col++) {
                computeMinimumPosition(row, col);
                temp = A[minRow][minCol];
                A[minRow][minCol] = A[row][col];
                A[row][col] = temp;
            }
        }
    }
}

public class ISC2012Q3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("INPUT:\tM = ");
        int M = in.nextInt();
        System.out.print("\tN = ");
        int N = in.nextInt();
        if (M > 2 && N > 2 && M < 20 && N < 20) {
            Matrix obj = new Matrix(M, N);
            obj.getInput();
            System.out.println("OUPUT:");
            System.out.println("ORIGINAL MATRIX");
            System.out.println(obj);
            obj.showMaxDetails();
            obj.showMinDetails();
            obj.sortAscending();
            System.out.println("REARRANGED MATRIX");
            System.out.println(obj);
        } else {
            System.out.println("OUTPUT:\n\tSIZE OUT OF RANGE");
        }

        in.close();
    }
}

All these solutions in one free book

Pragmatic Solutions to ISC Computer Science Practical Problems collects every program from 2004 to 2021 in a 202-page PDF.

Get the free PDF ₹0 ISC 2027 mock papers