Solved 2019 Practical Paper ISC Computer Science — Question 2
Back to all questionsQuestion 2
Write a program to declare a single-dimensional array a[] and a square matrix b[][] of size N, where N > 2 and N < 10. Allow the user to input positive integers into the single dimensional array.
Perform the following tasks on the matrix:
- Sort the elements of the single-dimensional array in ascending order using any standard sorting technique and display the sorted elements.
- Fill the square matrix b[][] in the following format:
If the array a[] = {5, 2, 8, 1} then, after sorting a[] = {1, 2, 5, 8}
Then, the matrix b[][] would fill as below:
- Display the filled matrix in the above format.
Test your program for the following data and some random data:
Example 1
INPUT:
N = 3
ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 3 1 7
OUTPUT:
SORTED ARRAY: 1 3 7
FILLED MATRIX
Example 2
INPUT:
N = 13
OUTPUT:
MATRIX SIZE OUT OF RANGE
Example 3
INPUT:
N = 5
ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 10 2 5 23 6
OUTPUT:
SORTED ARRAY: 2 5 6 10 23
FILLED MATRIX
Solution
import java.util.Scanner;
public class Array
{
public static void sortArray(int arr[]) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int t = arr[j];
arr[j] = arr[j+1];
arr[j+1] = t;
}
}
}
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("ENTER VALUE OF N: ");
int n = in.nextInt();
if (n <= 2 || n >= 10) {
System.out.println("MATRIX SIZE OUT OF RANGE");
return;
}
int a[] = new int[n];
int b[][] = new int[n][n];
System.out.println("ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY:");
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
sortArray(a);
System.out.println("SORTED ARRAY:");
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
for (int i = n - 1, r = 0; i >= 0; i--, r++) {
for (int j = 0; j <= i; j++) {
b[r][j] = a[j];
}
for (int k = n - 1; k > i; k--) {
b[r][k] = a[k - i - 1];
}
}
System.out.println();
System.out.println("FILLED MATRIX:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(b[i][j] + " ");
}
System.out.println();
}
}
}Output



import
java.util.Scanner
;
public
class
Array
{
public
static
void
sortArray
(
int
arr
[]) {
int
n
=
arr
.
length;
for
(
int
i
=
0
; i
<
n
-
1
; i
++
) {
for
(
int
j
=
0
; j
<
n
-
i
-
1
; j
++
) {
if
(arr[j]
>
arr[j
+
1
]) {
int
t
=
arr[j];
arr[j]
=
arr[j
+
1
];
arr[j
+
1
]
=
t;
}
}
}
}
public
static
void
main
(
String
args
[]) {
Scanner
in
=
new
Scanner
(
System
.
in);
System
.
out
.
print(
"
ENTER VALUE OF N:
"
);
int
n
=
in
.
nextInt();
if
(n
<=
2
||
n
>=
10
) {
System
.
out
.
println(
"
MATRIX SIZE OUT OF RANGE
"
);
return
;
}
int
a[]
=
new
int
[n];
int
b[][]
=
new
int
[n][n];
System
.
out
.
println(
"
ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY:
"
);
for
(
int
i
=
0
; i
<
n; i
++
) {
a[i]
=
in
.
nextInt();
}
sortArray(a);
System
.
out
.
println(
"
SORTED ARRAY:
"
);
for
(
int
i
=
0
; i
<
n; i
++
) {
System
.
out
.
print(a[i]
+
"
"
);
}
for
(
int
i
=
n
-
1
, r
=
0
; i
>=
0
; i
--
, r
++
) {
for
(
int
j
=
0
; j
<=
i; j
++
) {
b[r][j]
=
a[j];
}
for
(
int
k
=
n
-
1
; k
>
i; k
--
) {
b[r][k]
=
a[k
-
i
-
1
];
}
}
System
.
out
.
println();
System
.
out
.
println(
"
FILLED MATRIX:
"
);
for
(
int
i
=
0
; i
<
n; i
++
) {
for
(
int
j
=
0
; j
<
n; j
++
) {
System
.
out
.
print(b[i][j]
+
"
"
);
}
System
.
out
.
println();
}
}
}
Output