Write a program to declare a square matrix A[][] of order (M x 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:
- Sort the non-boundary elements in ascending order using any standard sorting technique and rearrange them in the matrix.
- Calculate the sum of both the diagonals.
- Display the original matrix, rearranged matrix and only the diagonal elements of the rearranged matrix with their sum.
Test your program with the sample 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.util.*;
class ISCprac2016Q02
{
public static void main(String arg[])throws InputMismatchException
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows for a square matrix (between 3 and 10): ");
int M = sc.nextInt();
if(M <=3 || M >=10){
System.out.println("THE MATRIX SIZE IS OUT OF RANGE.");
}else{
int a[][] = new int[M][M];
int b[] = new int[M*M];
int i, j, c, t;
System.out.println("Enter " + (M*M) + " elements");
for(i = 0; i < M; i++){
for(j = 0; j < M; j++){
a[i][j] = sc.nextInt();
}//loop j
}//loop i
System.out.println("\nORIGINAL MATRIX: ");
c = 0;
for(i = 0; i < M; i++){
for(j = 0; j < M; j++){
//print the elements of the original matrix
System.out.print(a[i][j]+ " ");
//store the non-boundary elements in an array
if(i != 0 && j != 0 && i != M-1 && j != M - 1)
b[c++] = a[i][j];
}//loop j
System.out.println();
}//loop i
//sort the non-boundary elements in ascending order
for(i = 0; i < c; i++){
for(j = i+1; j < c; j++){
if(b[i] > b[j]){
t = b[i];
b[i] = b[j];
b[j] = t;
}
}//loop j
}//loop i
c = 0;
for(i = 0; i < M; i++){
for(j = 0; j < M; j++){
//store the sorted elements at the non-boundary indeces
if(i != 0 && j != 0 && i != M-1 && j != M - 1)
a[i][j] = b[c++];
}//loop j
}//loop i
System.out.println("\nREARRANGED MATRIX: ");
//Display the rearranged matrix
for(i = 0; i < M; i++){
for(j = 0; j < M; j++){
System.out.print(a[i][j]+ " ");
}//loop j
System.out.println();
}//loop i
System.out.println("\nDIAGONAL ELEMENTS: ");
//Display the rearranged matrix's diagonal elements
for(i = 0; i < M; i++){
for(j = 0; j < M; j++){
if( i == j || i+j == M-1)
System.out.print(a[i][j]+ " ");
else
System.out.print(" ");
}//loop j
System.out.println();
}//loop i
}
}//end of main
}//end of class
Have something to say? Log in to comment on this post.
0 comments