ISC practical papers › ISC 2020 › Question 2
2D Array Rows as Octal Number
ISC 2020 Computer Science Practical, Question 2 · Arrays and matrices, Number programs
The question
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. 2*8^2 + 3X8^1 + 1*8^0) 4 0 5 (decimal equivalent of 1st row = 261 i.e. 4*8^2 + 0X8^1 + 5*8^0) 1 5 6 (decimal equivalent of 1st row = 110 i.e. 1*8^2 + 5X8^1 + 6*8^0)
Perform the following tasks on the matrix: (a) Display the original matrix (b) Calculate the decimal equivalent for each row and display as per the format given below.
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
Question: ISC 2020 Computer Science Paper 2 (Practical), Question 2, set by the Council for the Indian School Certificate Examinations (CISCE). Solution and video: Vinay Singh.
Java solution
import java.util.*;
class Matrix {
private int M, N, A[][];
public Matrix(int M, int N) {
this.M = M;
this.N = N;
A = new int[M][N];
}
public void input() {
Scanner in = new Scanner(System.in);
for (int row = 0; row < M; row++) {
System.out.print("ENTER ELEMENTS FOR ROW " + (row + 1) + ": ");
for (int col = 0; col < N; col++) {
A[row][col] = in.nextInt();
if (!(A[row][col] >= 0 && A[row][col] <= 7)) {
System.out.println("OUTPUT\nINVALID INPUT");
System.exit(1);
}
}
}
in.close();
}
public void display() {
int decimal;
System.out.println("\nFILLED MATRIX\tDECIMAL EQUIVALENT");
for (int row = 0; row < M; row++) {
decimal = 0;
for (int col = 0; col < N; col++) {
System.out.print(A[row][col] + " ");
decimal += A[row][col] * Math.pow(8, N - col - 1);
}
System.out.print("\b\t\t" + decimal + "\n");
}
}
}
public class ISC2020Q2 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int M, N;
System.out.println("INPUT");
System.out.print("M = ");
M = in.nextInt();
System.out.print("N = ");
N = in.nextInt();
if (M > 0 && M < 10 && N > 2 && N < 6) {
Matrix A = new Matrix(M, N);
A.input();
System.out.println("OUTPUT");
A.display();
} else {
System.out.println("OUTPUT\nOUT OF RANGE");
}
in.close();
}
}
All these solutions in one free book
Pragmatic Solutions to ISC Computer Science Practical Problems collects every program from 2004 to 2021 in a 202-page PDF.