The following function magicfun() is a part of some class.
What will the function magicfun()
return, when the value of n=2 and n=10, respectively? Show the dry run/working:
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.
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.
import [Link];
public class Sort
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("ENTER MATRIX SIZE (M): ");
int m = [Link]();
if (m <= 3 || m >= 10) {
[Link]("THE MATRIX SIZE IS OUT OF RANGE.");
return;
int a[][] = new int[m][m];
[Link]("ENTER ELEMENTS OF MATRIX");
for (int i = 0; i < m; i++) {
[Link]("ENTER ROW " + (i+1) + ":");
for (int j = 0; j < m; j++) {
a[i][j] = [Link]();
if (a[i][j] < 0) {
[Link]("INVALID INPUT");
return;
}
[Link]("ORIGINAL MATRIX");
printMatrix(a, m);
sortNonBoundaryMatrix(a, m);
[Link]("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;
[Link]("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];
[Link](a[i][j] + "\t");
else {
[Link]("\t");
[Link]();
}
[Link]("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++) {
[Link](a[i][j] + "\t");
[Link]();
k=0
b[]=new int[(m-2)*(m-2)];
for(i=1;i<m-1;i++)
For(j=1;j<m-1;j++)
b[k]=a[i][j];
K++;
If(i==j||i+j==m-1)
Sum+
for(i=0;i<k-1;i++)
For(j=0;j<k-i-1;j++)
If(b[j]>b[j+1])
k=0
for(i=1;i<m-1;i++)
For(j=1;j<m-1;j++)
a[i][j] =b[k];
k++;
}
For(i=0;i<m;i++)
For(j=0;j<m;j++)
If(i==0||j==0||i==m-1||j==m-1)
Sum+=a[i][j];
S.o.p(a[i][j]);
}
For(i=0;i<3;i++)
{
For(j=0;j<3;j++)
{
C[i][j]=0;
For(k=0;k<3;k++)
{
C[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}