C Programs Tutorials | IT Developer
IT Developer

Java Programs - Advanced



Share with a Friend

Sort Non-boundary Matrix Elements Program - Java Program

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.

import java.io.*; class MatrixSort{ public static void main(String args[]) throws IOException{ InputStreamReader in = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(in); System.out.print("M = "); int m = Integer.parseInt(br.readLine()); 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++) a[i][j] = Math.abs(Integer.parseInt(br.readLine())); 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); } }

Output

 
OUTPUT :

M = 4
Enter positive integers:
9
2
1
5
8
13
8
4
15
6
3
11
7
2
23
7
ORIGINAL MATRIX
9   2   1   5   
8   13  8   4   
15  6   3   11  
7   2   23  7   
REARRANGED MATRIX
9   2   1   5   
8   3   6   4   
15  8   13  11  
7   2   23  7   
DIAGONAL ELEMENTS
9           5   
    3   6       
    8   13      
7           7   
SUM OF THE DIAGONAL ELEMENTS = 58