Code with Vinay

ISC practical papers › ISC 2016 › Question 2

Sort non boundary elements

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

The question

Write a program to declare a square matrix A[][] of order (M x M) where ‘M’ must be greater than 3 and less than 10. Allow the user to input positive integers into this matrix.

Perform the following tasks on the matrix: (a) Sort the non-boundary elements in ascending order using any standard sorting technique and rearrange them in the matrix. (b) Calculate the sum of both the diagonals. (c) Display the original matrix, rearranged matrix and only the diagonal elements of the rearranged matrix with their sum.

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

Example 1

INPUT:  M = 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
        9   2   1   5
        8   3   6   4
        15  8   13  11
        7   12  23  8
DIAGONAL ELEMENTS
        9           5
            3   6
            8   13
        7           8

SUM OF THE DIAGONAL ELEMENTS = 59

Example 2

INPUT:  M = 5
        7   4   1   9   5
        8   2   6   10  19
        13  1   3   4   1
        10  0   5   12  16
        1   8   17  6   8
OUTPUT:
ORIGINAL MATRIX
        7   4   1   9   5
        8   2   6   10  19
        13  1   3   4   1
        10  0   5   12  16
        1   8   17  6   8
REARRANGED MATRIX
        7   4   1   9   5
        8   0   1   2   19
        13  3   4   5   1
        10  6   10  12  16
        1   8   17  6   8
DIAGONAL ELEMENTS
        7               5
            0       2
                 4
            6       12
        1               8

SUM OF THE DIAGONAL ELEMENTS = 45

INPUT:  M = 3
OUTPUT: THE MATRIX IS OUT OF RANGE

Question: ISC 2016 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

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

class Matrix {
    private int M, A[][];

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

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

    public void display() {
        for (int row = 0; row < M; row++) {
            System.out.print("\t");
            for (int col = 0; col < M; col++) {
                System.out.print(A[row][col]);
                if (col < M - 1)
                    System.out.print("\t");
            }
            System.out.println();
        }
    }

    public void displayDiagonals() {
        for (int row = 0; row < M; row++) {
            System.out.print("\t");
            for (int col = 0; col < M; col++) {
                if (row == col || row + col == M - 1) {
                    System.out.print(A[row][col]);
                }
                if (col < M - 1)
                    System.out.print("\t");
            }
            System.out.println();
        }
    }

    public void displaySumOfDiagonals() {
        System.out.println("SUM OF THE DIAGONAL ELEMENTS = " + getSumOfDiagonals(A));
    }

    private static int getSumOfDiagonals(int A[][]) {
        int sum = 0;
        for (int i = 0; i < A.length; i++) {
            sum += A[i][i] + A[i][A.length - 1 - i];
        }
        return (A.length % 2 == 1) ? sum - A[A.length / 2][A.length / 2] : sum;
    }

    public void sort() {
        int temp[] = new int[(M - 2) * (M - 2)], index = 0, buffer;
        for (int row = 1; row < M - 1; row++) {
            for (int col = 1; col < M - 1; col++) {
                temp[index++] = A[row][col];
            }
        }
        boolean swapped;
        for (int pass = 0; pass < temp.length - 1; pass++) {
            swapped = false;
            for (int x = 0; x < temp.length - 1 - pass; x++) {
                if (temp[x + 1] < temp[x]) {
                    buffer = temp[x + 1];
                    temp[x + 1] = temp[x];
                    temp[x] = buffer;
                    swapped = true;
                }
            }
            if (!swapped)
                break;
        }

        index = 0;
        for (int row = 1; row < M - 1; row++) {
            for (int col = 1; col < M - 1; col++) {
                A[row][col] = temp[index++];
            }
        }
    }

}

public class ISC2016Q2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("INPUT:\tM = ");
        int M = in.nextInt();
        if (M <= 3 || M >= 10) {
            System.out.println("OUTPUT:\tTHE MATRIX IS OUT OF RANGE");
        } else {
            Matrix A = new Matrix(M);
            A.input();
            System.out.println("OUTPUT:");
            System.out.println("ORIGINAL MATRIX");
            A.display();
            System.out.println("REARRANGED MATRIX");
            A.sort();
            A.display();
            System.out.println("DIAGONAL ELEMENTS");
            A.displayDiagonals();
            A.displaySumOfDiagonals();

        }

        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