Code with Vinay

ISC practical papers › ISC 2019 › Question 2

2D Array Pattern Problem

ISC 2019 Computer Science Practical, Question 2 · Arrays and matrices

The question

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: (a) Sort the elements of the single dimensional array in ascending order using any standard sorting technique and display the sorted elements.

(b) Fill the square matrixb[][]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:
1    2    5    8
1    2    5    1
1    2    1    2
1    1    2    5

(c) 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
1    3    7
1    3    1
1    1    3

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
2    5    6    10    23
2    5    6    10    2
2    5    6    2    5
2    5    2    5    6
2    2    5    6    10

Question: ISC 2019 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

ISC2019Q2.java Download GitHub
import java.util.*;

class ISC2019Q2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("INPUT");
        System.out.print("N = ");
        int N = in.nextInt();
        if (N > 2 && N < 10) {
            int a[] = new int[N];
            System.out.print("ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: ");
            for (int i = 0; i < N; i++) {
                a[i] = in.nextInt();
            }
            bubbleSort(a);
            System.out.println("OUTPUT");
            System.out.print("SORTED ARRAY: \nFILLED MATRIX");
            display(a);
            int b[][] = createAndFillMatrix(a);
            display(b);
        } else {
            System.out.println("OUTPUT\nMATRIX SIZE OUT OF RANGE");
        }
        in.close();
    }

    public static int[][] createAndFillMatrix(int a[]) {
        int b[][] = new int[a.length][a.length];
        int offset = 0;
        for (int row = 0; row < b.length; row++) {
            offset = 0;
            for (int col = 0; col < b[row].length; col++) {
                if (col < b.length - row) {
                    b[row][col] = a[col];
                } else {
                    b[row][col] = a[offset++];
                }
            }
        }
        return b;
    }

    public static void bubbleSort(int a[]) {
        int temp;
        for (int pass = 0; pass < a.length - 1; pass++) {
            for (int x = 0; x < a.length - 1 - pass; x++) {
                if (a[x + 1] < a[x]) {
                    temp = a[x + 1];
                    a[x + 1] = a[x];
                    a[x] = temp;
                }
            }
        }
    }

    public static void display(int a[]) {
        for (int n : a) {
            System.out.print(n + " ");
        }
        System.out.println("\b");
    }

    public static void display(int b[][]) {
        for (int row = 0; row < b.length; row++) {
            for (int col = 0; col < b[row].length; col++) {
                System.out.print(b[row][col] + "\t");
            }
            System.out.println("\b");
        }
    }
}

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.

Get the free PDF ₹0 ISC 2027 mock papers