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: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

OUTPUT:
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.
```
```

# Share code with your friends

Code

```				```
import java.util.Scanner;
public class SortMatrix
{
public static void main(String args[])
{
int i=0,j=0,k=0,l=0,m=0,temp=0,sum=0;
int arr[][];
Scanner sc=new Scanner(System.in);

System.out.print("M = ");
m = sc.nextInt();
if(m < 4 || m > 9)
{
System.out.println("THE MATRIX SIZE IS OUT OF RANGE.");
return;
}
arr = new int[m][m];
System.out.println("Enter positive integers:");
for(i = 0; i < m; i++)
{
for(j = 0; j < m; j++)
{
arr[i][j] = Math.abs(sc.nextInt());
}
}
System.out.println("ORIGINAL MATRIX");
for(i = 0; i < m; i++)
{
for( j = 0; j < m; j++)
{
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
/*Rearranging of matrix starts here*/

/*IN the code we have four loops first two loops are row and columns of matrix respectively*/

/*the other two loops inside 'j' loop do the same thing.They also represent rows and columns.*/
/*it is done so that arr[i][j] remains constants while we check it with all the non-boundary elements which is represented by arr[k][l]*/
for( i = 1; i < m - 1; i++)   /*i loop*/
{
for(j = 1; j < m - 1; j++) /*j loop*/
{
for(k=1;k< m-1;k++)/*k loop*/
{
for(l=1;l< m-1;l++)/*l loop*/
{
if(arr[i][j]< arr[k][l])
{
temp=arr[i][j];
arr[i][j]=arr[k][l];
arr[k][l]=temp;
}
}
}
}
}
/*Rearranging of matrix ends here*/

System.out.println("REARRANGED MATRIX");
for(i = 0; i < m; i++)
{
for(j = 0; j < m; j++)
{
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}

System.out.println("DIAGONAL ELEMENTS");
for(i = 0; i < m; i++)
{
for(j = 0; j < m; j++)
{
if(i == j || i + j == m - 1)
{
System.out.print(arr[i][j] + "\t");
sum += arr[i][j];
}
else
{
System.out.print("\t");
}
}
System.out.println();
}
System.out.println("SUM OF THE DIAGONAL ELEMENTS = " + sum);
}
}

```
```

Sale

50
Sale

150
Sale

200
Sale

50
Sale

50
Sale

50
Sale

50
Sale

50
Sale

50