Code with Vinay

ISC practical papers › ISC 2018 › Question 2

Sorting Rows Of A 2D Array

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

The question

Write a program to declare a matrix A[][] of order (MxN) where 'M' is the number of rows and 'N' is the number of coloumns such that the values of both 'M' and 'N' must be greater than 2 and less than 10. Allow the user to input integers into this matrix. Perform the following tasks on the matrix: (a) Display the original matrix. (b) Sort each row of the matrix in ascending order using any standard sorting technique. (c) Display the changed matrix after sorting each row.

Test your program for the following data:
Example 1:
INPUT: M=3
       N=3
ENTER ELEMENTS OF MATRIX
11 -2 3
5 16 7
9 0 4
3 1 8
OUTPUT:
ORIGNAL MATRIX
11  -2  3
5   16  7
9   0   4
3   1   8
MATRIX AFTER SORTING
-2  3   11
5   7   16
0   4   0
1   3   8
Example 2:
INPUT: M=3
       N=3
ENTER ELEMENTS OF MATRIX
22  5   19
7   36  12
9   13  6
OUTPUT:
ORIGINAL MATRIX
22  5   19
7   36  12
9   13  6
MATRIX AFTER SORTING ROWS
5   19  22
7   12  36
6   9   13
Example 3:
INPUT: M=11
       N=5
OUTPUT:
MATRIX OUT OF RANGE

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

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

class ISC2018Q2 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("INPUT: M = ");
        int M = in.nextInt();
        System.out.println("       N = ");
        int N = in.nextInt();
        if (M > 2 && M < 10 && N > 2 && N < 10) {
            int A[][] = new int[M][N];
            input(A);
            display(A, "ORIGINAL MATRIX");
            for( int row=0; row<M;row++){
                sort(A[row]);
            }
            display(A, "MATRIX AFTER SORTING ROWS");

        } else {
            System.out.println("OUTPUT:\nMATRIX OUT OF RANGE");
        }
        in.close();
    }

    public static void input(int A[][]) {
        System.out.println("ENTER ELEMENTS OF MATRIX");
        Scanner in = new Scanner(System.in);
        for (int row = 0; row < A.length; row++) {
            for (int col = 0; col < A[row].length; col++) {
                A[row][col] = in.nextInt();
            }
        }
        in.close();
    }

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

    // public static void sort(int A[][]){
    // int temp;
    // boolean swapped;
    // for( int row=0; row<A.length;row++){
    // for(int pass=0; pass<A[row].length-1;pass++){
    // swapped=false;
    // for(int x=0; x<A[row].length-1-pass;x++){
    // if(A[row][x+1]<A[row][x]){
    // temp=A[row][x+1];
    // A[row][x+1]=A[row][x];
    // A[row][x]=temp;
    // swapped=true;
    // }
    // }
    // if(!swapped) break;
    // }
    // }
    // }
    public static void sort(int A[]) {
        int temp;
        boolean swapped;

        for (int pass = 0; pass < A.length - 1; pass++) {
            swapped = false;
            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;
                    swapped = true;
                }
            }
            if (!swapped)
                break;
        }

    }
}

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