Categories

# Matrix Sorting ISC 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

`9    2    1    58    13   8    415   6    3    117    12   23   8`

OUTPUT:
ORIGINAL MATRIX

`9    2    1    58    13   8    415   6    3    117    12   23   8`

REARRANGED MATRIX

`9    2    1    58    3    6    415   8    13   117    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    58    2    6    10   1913   1    3    5    110   0    5    12   161    8    17   6    8`

OUTPUT:
ORIGINAL MATRIX

`7    4    1    9    58    2    6    10   1913   1    3    5    110   0    5    12   161    8    17   6    8`

REARRANGED MATRIX

`7    4    1    9    58    0    1    2    1913   3    5    5    110   6    10   12   161    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.io.*;
class MatrixSort{
public static void main(String args[])
throws IOException{
System.out.print("M = ");
if(m < 4 || m > 9){
System.out.println("THE MATRIX SIZE IS OUT OF RANGE.");
return;
}
int a[][] = new int[m][m];
System.out.println("Enter positive integers:");
for(int i = 0; i < m; i++)
for(int j = 0; j < m; j++)
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();
}
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 < 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;
}
}
}
k = 0;
for(int i = 1; i < m - 1; i++){
for(int j = 1; j < m - 1; j++){
a[i][j] = b[k++];
}
}
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();
}
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){
System.out.print(a[i][j] + "\t");
sum += a[i][j];
}
else
System.out.print("\t");
}
System.out.println();
}
System.out.println("SUM OF THE DIAGONAL ELEMENTS = " + sum);
}
}``````

## 2 replies on “Matrix Sorting ISC 2016 Practical”

sayan ranasays:

Sir,a program is given which states that:
Write a program in java 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. Now swap the elements between the left and right diagonal.