Solved 2020 Practical Paper ISC Computer Science — Question 2
Back to all questionsQuestion 2
Write a program to declare a matrix A[][] of order (M x N) where 'M' is the number of rows and 'N' is the number of columns such that the value of 'M' must be greater than 0 and less than 10 and the value of 'N' must be greater than 2 and less than 6. Allow the user to input digits (0 - 7) only at each location, such that each row represents an octal number.
Example:
| 2 | 3 | 1 | (decimal equivalent of 1st row = 153 i.e. 2x82 + 3x81 + 1x80) |
| 4 | 0 | 5 | (decimal equivalent of 2nd row = 261 i.e. 4x82 + 0x81 + 5x80) |
| 1 | 5 | 6 | (decimal equivalent of 3rd row = 110 i.e. 1x82 + 5x81 + 6x80) |
Perform the following tasks on the matrix:
- Display the original matrix.
- Calculate the decimal equivalent for each row and display as per the format given below.
Test your program for the following data and some random data:
Example 1:
INPUT:
M = 1
N = 3
ENTER ELEMENTS FOR ROW 1: 1 4 4
OUTPUT:
| FILLED MATRIX | DECIMAL EQUIVALENT | ||
|---|---|---|---|
| 1 | 4 | 4 | 100 |
Example 2:
INPUT:
M = 3
N = 4
ENTER ELEMENTS FOR ROW 1: 1 1 3 7
ENTER ELEMENTS FOR ROW 2: 2 1 0 6
ENTER ELEMENTS FOR ROW 3: 0 2 4 5
OUTPUT:
| FILLED MATRIX | DECIMAL EQUIVALENT | |||
|---|---|---|---|---|
| 1 | 1 | 3 | 7 | 607 |
| 2 | 1 | 0 | 6 | 1094 |
| 0 | 2 | 4 | 5 | 165 |
Example 3:
INPUT:
M = 3
N = 3
ENTER ELEMENTS FOR ROW 1: 2 4 8
OUTPUT:
INVALID INPUT
Example 4:
INPUT:
M = 4
N = 6
OUTPUT:
OUT OF RANGE
Solution
import java.util.Scanner;
public class OctalMatrix
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of rows (M): ");
int m = in.nextInt();
System.out.print("Enter the number of columns (N): ");
int n = in.nextInt();
if (m <= 0 || m >= 10 || n <= 2 || n >= 6) {
System.out.println("OUT OF RANGE");
return;
}
int a[][] = new int[m][n];
for (int i = 0; i < m; i++) {
System.out.println("ENTER ELEMENTS FOR ROW " + (i + 1) + ": ");
for (int j = 0; j < n; j++) {
a[i][j] = in.nextInt();
if (a[i][j] < 0 || a[i][j] > 7) {
System.out.println("INVALID INPUT");
return;
}
}
}
System.out.println("FILLED MATRIX\tDECIMAL EQUIVALENT");
for (int i = 0; i < m; i++) {
int decNum = 0;
for (int j = 0; j < n; j++) {
decNum += a[i][j] * Math.pow(8, n - j - 1 );
System.out.print(a[i][j] + " ");
}
System.out.print("\t\t" + decNum);
System.out.println();
}
}
}Output




import
java.util.Scanner
;
public
class
OctalMatrix
{
public
static
void
main
(
String
args
[]) {
Scanner
in
=
new
Scanner
(
System
.
in);
System
.
out
.
print(
"
Enter the number of rows (M):
"
);
int
m
=
in
.
nextInt();
System
.
out
.
print(
"
Enter the number of columns (N):
"
);
int
n
=
in
.
nextInt();
if
(m
<=
0
||
m
>=
10
||
n
<=
2
||
n
>=
6
) {
System
.
out
.
println(
"
OUT OF RANGE
"
);
return
;
}
int
a[][]
=
new
int
[m][n];
for
(
int
i
=
0
; i
<
m; i
++
) {
System
.
out
.
println(
"
ENTER ELEMENTS FOR ROW
"
+
(i
+
1
)
+
"
:
"
);
for
(
int
j
=
0
; j
<
n; j
++
) {
a[i][j]
=
in
.
nextInt();
if
(a[i][j]
<
0
||
a[i][j]
>
7
) {
System
.
out
.
println(
"
INVALID INPUT
"
);
return
;
}
}
}
System
.
out
.
println(
"
FILLED MATRIX
\t
DECIMAL EQUIVALENT
"
);
for
(
int
i
=
0
; i
<
m; i
++
) {
int
decNum
=
0
;
for
(
int
j
=
0
; j
<
n; j
++
) {
decNum
+=
a[i][j]
*
Math
.
pow(
8
, n
-
j
-
1
);
System
.
out
.
print(a[i][j]
+
"
"
);
}
System
.
out
.
print(
"
\t\t
"
+
decNum);
System
.
out
.
println();
}
}
}
Output