ICSE Class 12 Computer Science Question 2 of 3

Solved 2016 Practical Paper ISC Computer Science — Question 2

Back to all questions
2
Question

Question 2

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.

Solution
import java.util.Scanner;

public class MatrixSort
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("ENTER MATRIX SIZE (M): ");
        int m = in.nextInt();

        if (m <= 3 || m >= 10) {
            System.out.println("THE MATRIX SIZE IS OUT OF RANGE.");
            return;
        }

        int a[][] = new int[m][m];

        System.out.println("ENTER ELEMENTS OF MATRIX");
        for (int i = 0; i < m; i++) {
            System.out.println("ENTER ROW " + (i+1) + ":");
            for (int j = 0; j < m; j++) {
                a[i][j] = in.nextInt();
                /*
                 * Only positive integers 
                 * should be enetered
                 */
                if (a[i][j] < 0) {
                    System.out.println("INVALID INPUT");
                    return;
                }
            }
        }
        
        System.out.println("ORIGINAL MATRIX");
        printMatrix(a, m);
        
        sortNonBoundaryMatrix(a, m);
        System.out.println("REARRANGED MATRIX");
        printMatrix(a, m);
        
        computePrintDiagonalSum(a, m);
    }

    public static void sortNonBoundaryMatrix(int a[][], int m) {
        /*
         * To sort non-boundary elements of Matrix
         * copy the non-boundary elements in a
         * single dimensional array. Sort the 
         * single dimensional array and assign the
         * sorted elements back in the 2D array at
         * their respective positions
         */
        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;
        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) {
                    sum += a[i][j];
                    System.out.print(a[i][j] + "\t");
                }
                else {
                    System.out.print("\t");
                }
            }
            System.out.println();
        }
        System.out.println("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++) {
                System.out.print(a[i][j] + "\t");
            }
            System.out.println();
        }
    }
}
Output
BlueJ output of MatrixSort.java
BlueJ output of MatrixSort.java
BlueJ output of MatrixSort.java
Answer

import
java.util.Scanner
;
public
class
MatrixSort
{
public
static
void
main
(
String
args
[]) {
Scanner
in
=
new
Scanner
(
System
.
in);
System
.
out
.
print(
"
ENTER MATRIX SIZE (M):
"
);
int
m
=
in
.
nextInt();
if
(m
<=
3
||
m
>=
10
) {
System
.
out
.
println(
"
THE MATRIX SIZE IS OUT OF RANGE.
"
);
return
;
}
int
a[][]
=
new
int
[m][m];
System
.
out
.
println(
"
ENTER ELEMENTS OF MATRIX
"
);
for
(
int
i
=
0
; i
<
m; i
++
) {
System
.
out
.
println(
"
ENTER ROW
"
+
(i
+
1
)
+
"
:
"
);
for
(
int
j
=
0
; j
<
m; j
++
) {
a[i][j]
=
in
.
nextInt();
/*
* Only positive integers
* should be enetered
*/
if
(a[i][j]
<
0
) {
System
.
out
.
println(
"
INVALID INPUT
"
);
return
;
}
}
}
System
.
out
.
println(
"
ORIGINAL MATRIX
"
);
printMatrix(a, m);

sortNonBoundaryMatrix(a, m);
System
.
out
.
println(
"
REARRANGED MATRIX
"
);
printMatrix(a, m);

computePrintDiagonalSum(a, m);
}
public
static
void
sortNonBoundaryMatrix
(
int
a
[][],
int
m
) {
/*
* To sort non-boundary elements of Matrix
* copy the non-boundary elements in a
* single dimensional array. Sort the
* single dimensional array and assign the
* sorted elements back in the 2D array at
* their respective positions
*/
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
;
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
) {
sum
+=
a[i][j];
System
.
out
.
print(a[i][j]
+
"
\t
"
);
}
else
{
System
.
out
.
print(
"
\t
"
);
}
}
System
.
out
.
println();
}
System
.
out
.
println(
"
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
++
) {
System
.
out
.
print(a[i][j]
+
"
\t
"
);
}
System
.
out
.
println();
}
}
}
Output