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
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 | 5 | 1 |
10 | 0 | 5 | 12 | 16 |
1 | 8 | 17 | 6 | 8 |
ORIGINAL MATRIX
7 | 4 | 1 | 9 | 5 |
8 | 2 | 6 | 10 | 19 |
13 | 1 | 3 | 5 | 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 | 5 | 5 | 1 |
10 | 6 | 10 | 12 | 16 |
1 | 8 | 17 | 6 | 8 |
DIAGONAL ELEMENTS
7 | 5 | |||
0 | 2 | |||
5 | ||||
6 | 12 | |||
1 | 8 |
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);
}
}