Code with Vinay

ISC practical papers › ISC 2009 › Question 2

2D Array: Sort Boundary Elements

ISC 2009 Computer Science Practical, Question 2 · Arrays and matrices

The question

Write a program to declare a matrix A[][] of order M x 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 positive integers into this matrix. Perform the following tasts on the matrix: a) Sort the elements of the outer rows and columns in ascending order using any standard sorting technique and arrange them in an array. b) Calculate the sum of the outer row and column elements. c) Output the original matrix, rearranged matrix and only the border elements of the rearranged array with their sum.

Test your program for the following data and some random data:
Example 1:
INPUT:  M = 3
        N = 3
        1   7   4
        8   2   5
        6   3   9
OUTPUT:
        ORIGINAL MATRIX
        1   7   4
        8   2   5
        6   3   9
        REARRANGED MATRIX
        1   3   4
        9   2   5
        8   7   6
        BOUNDARY ELEMENTS
        1   3   4
        9       5
        8   7   6
        SUM OF THE OUTER ROW AND COLUMN ELEMENTS = 43
Example 2:
INPUT:   M = 2
         N = 3
         7  1  6
         8  9  2
OUTPUT:
                ORIGINAL  MATRIX
         7  1  6
         8  9  2
         REARRANGED  MATRIX
         1  2  6
         9  8  7
         BOUNDARY  ELEMENTS
         1  2  6
         9  8  7
         SUM OF THE OUTER ROW AND COLUMN ELEMENTS  =  33
Example 4:
INPUT:   M = 4
         N = 4
         9  2  1  5
         8  13 8  4
         15 6  3  11
         7  12 23 8
OUTPUT:
         ORIGINAL  MATRIX
         9  2  1  5
         8  13 8  4
         15 6  3  11
         7  12 23 8
         REARRANGED  MATRIX
         1  2  4  5
         23 13 8  7
         15 6  3  8
         12 11 9  8
         BOUNDARY  ELEMENTS
         1  2  4  5
         23       7
         15       8
         12 11 9  8
         SUM OF THE OUTER ROW AND COLUMN ELEMENTS  =  105

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

Java solution

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

class Matrix {
    private int A[][], row, col, M, N, outer[], sum;

    public Matrix(int M, int N) {
        this.M = M;
        this.N = N;
        A = new int[M][N];
        outer = new int[2 * N + 2 * (M - 2)];
    }

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

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

    public void rearrangeMatrix() {
        // Populating the 1D array outer
        int index = 0, sum = 0;
        for (col = 0; col < N; col++) {
            outer[index++] = A[0][col];
            outer[index++] = A[M - 1][col];
            sum += A[0][col] + A[M - 1][col];
        }
        for (row = 1; row < M - 1; row++) {
            outer[index++] = A[row][0];
            outer[index++] = A[row][N - 1];
            sum += A[row][0] + A[row][N - 1];
        }
        // Sort 1D array outer
        Arrays.sort(outer);

        // Place back the 1D array outer into A[][]
        index = 0;
        for (col = 0; col <= N - 1; col++) { // Tol L-R
            A[0][col] = outer[index++];
        }
        for (row = 1; row <= M - 1; row++) { // Right col T-B
            A[row][N - 1] = outer[index++];
        }
        for (col = N - 2; col >= 0; col--) { // Bottom row R-L
            A[M - 1][col] = outer[index++];
        }
        for (row = M - 2; row >= 1; row--) {
            A[row][0] = outer[index++];
        }
    }

    public void displayBoundaryElements() {
        String output = "";
        for (row = 0; row < M; row++) {

            for (col = 0; col < N; col++) {
                if (row == 0 || col == 0 || row == M - 1 || col == N - 1)
                    output += "\t"+A[row][col];
                else
                    output += "\t";
                
            }
            output += "\n";
        }
        System.out.println(output);
    }

    public int getSumOfOuterElements() {
        return sum;
    }
}

public class ISC2009Q2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int M, N;
        System.out.print("INPUT:\tM = ");
        M = in.nextInt();
        System.out.print("\tN = ");
        N = in.nextInt();
        Matrix obj = new Matrix(M, N);
        obj.input();
        System.out.println("OUTPUT:");
        System.out.println("\tORIGINAL MATRIX");
        System.out.println(obj);
        obj.rearrangeMatrix();
        System.out.println("\tREARRANGED  MATRIX");
        System.out.println(obj);
        System.out.println("\tBOUNDARY ELEMENTS");
        obj.displayBoundaryElements();
        System.out.println("\tSUM OF THE OUTER ROW AND COLUMN ELEMENTS  =  " + obj.getSumOfOuterElements());
        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