SUM OF DIAGONALS AND SORTING OF
NON-BOUNDARY ELEMENTS:
QUESTION:
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:
1. Sort the non-boundary elements in ascending order using any standard sorting
technique and rearrange them in the matrix.
2. Calculate the sum of both the diagonals.
3. Display the original matrix, rearranged matrix and only the diagonal elements of the
rearranged matrix with their sum.
PROGRAM:
import java.util.Scanner;
public class MatrixSort
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("ENTER MATRIX SIZE (M): ");
int m = in.nextInt();
if (m <= 3 || m >= 10) {
System.out.println("THE MATRIX SIZE IS OUT OF RANGE.");
return;
}
int a[][] = new int[m][m];
System.out.println("ENTER ELEMENTS OF MATRIX");
for (int i = 0; i < m; i++) {
System.out.println("ENTER ROW " + (i+1) + ":");
for (int j = 0; j < m; j++) {
a[i][j] = in.nextInt();
if (a[i][j] < 0) {
System.out.println("INVALID INPUT");
return;
}
}
}
System.out.println("ORIGINAL MATRIX");
printMatrix(a, m);
sortNonBoundaryMatrix(a, m);
System.out.println("REARRANGED MATRIX");
printMatrix(a, m);
computePrintDiagonalSum(a, m);
}
public static void sortNonBoundaryMatrix(int a[][], int m) {
int b[] = new int[(m - 2) * (m - 2)];
int k = 0;
for (int i = 1; i < m - 1; i++) {
for (int j = 1; j < m - 1; j++) {
b[k++] = a[i][j];
}
}
for (int i = 0; i < k - 1; i++) {
for (int j = 0; j < k - i - 1; j++) {
if (b[j] > b[j + 1]) {
int t = b[j];
b[j] = b[j+1];
b[j+1] = t;
}
}
}
k = 0;
for (int i = 1; i < m - 1; i++) {
for (int j = 1; j < m - 1; j++) {
a[i][j] = b[k++];
}
}
}
public static void computePrintDiagonalSum(int a[][], int m) {
int sum = 0;
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) {
sum += a[i][j];
System.out.print(a[i][j] + "\t");
}
else {
System.out.print("\t");
}
}
System.out.println();
}
System.out.println("SUM OF THE DIAGONAL ELEMENTS = " + sum);
}
public static void printMatrix(int a[][], int m) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
System.out.print(a[i][j] + "\t");
}
System.out.println();
}
}
}
OUTPUT:
VARIABLE DESCRIPTION TABLE:
S VARIABLE DATA TYPE DESCRIPTION
NO.
1 m int Size of the square matrix (number of rows/columns).
2 A[][] Int[][] 2D array representing the matrix.
3 B[] Int[] 1D array to hold non-boundary matrix elements for sorting.
4 k int Index variable for inserting elements into b[] or retrieving
them.
5 I,j int Loop variables used to iterate over rows and columns of the
matrix
6 t int Temporary variable used to swap values during sorting in
sortNonBoundaryMatrix.
7 sum int Stores the sum of diagonal elements of the matrix.
Methods Description: