Sort Non-boundary Elements Java Program | ISC Computer Science 2016 Practical

Write a program to declare a square matrix A[][] of order (M × 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 for the following data and some random data:

Example 1
INPUT:
M = 4

9215
81384
156311
712238

OUTPUT:
ORIGINAL MATRIX

9215
81384
156311
712238

REARRANGED MATRIX

9215
8364
1581311
712238

DIAGONAL ELEMENTS

95
36
813
78

SUM OF THE DIAGONAL ELEMENTS = 59

Example 2

INPUT:

M = 5

74195
8261019
131351
10051216
181768

ORIGINAL MATRIX

74195
8261019
131351
10051216
181768

REARRANGED MATRIX

74195
801219
133551
106101216
181768

DIAGONAL ELEMENTS

75
02
5
612
18

SUM OF THE DIAGONAL ELEMENTS = 46

Example 3
INPUT:
M = 3
OUTPUT:
THE MATRIX SIZE IS 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());
        if(m < 4 || m > 9){
            System.out.println("THE MATRIX SIZE IS OUT OF RANGE.");
            return;
        }
        int a[][] = new int[m][m];
        int b[] = new int[(m - 2) * (m - 2)];
        int index = 0;
        int d1 = 0;
        int d2 = 0;
        System.out.println("Enter matrix elements:");
        for(int i = 0; i < m; i++){
            for(int j = 0; j < m; j++){
                a[i][j] = Integer.parseInt(in.nextLine());
                if(i == j)
                    d1 += a[i][j];
                if(i + j == m - 1)
                    d2 += a[i][j];
                if(i == 0 || j == 0 || i == m - 1 || j == m - 1)
                    continue;
                else
                    b[index++] = a[i][j];
            }
        }
        int sum = d1 + d2;
        System.out.println("ORIGINAL MATRIX");
        for(int i = 0; i < m; i++){
            for(int j = 0; j < m; j++)
                System.out.print(a[i][j] + "\t");
            System.out.println();
        }
        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 < m; j++){
                if(i == 0 || j == 0 || i == m - 1 || j == m - 1)
                    continue;
                else
                    a[i][j] = b[index++];
            }
        }
        System.out.println("REARRANGED MATRIX");
        for(int i = 0; i < m; i++){
            for(int j = 0; j < m; j++)
                System.out.print(a[i][j] + "\t");
            System.out.println();
        }
        System.out.println("DIAGONAL ELEMENTS");
        for(int i = 0; i < m; i++){
            for(int j = 0; j < m; j++)
                if(i == j || i + j == m - 1)
                    System.out.print(a[i][j] + "\t");
                else
                    System.out.print("\t");
            System.out.println();
        }
        System.out.println("SUM OF THE DIAGONAL ELEMENTS = " + sum);
    }
}

Leave a Reply

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