Code with Vinay

ISC practical papers › ISC 2013 › Question 2

Mirror image of a 2D array

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

The question

Write a program to declare a square matrix A[][] of order (M X M) where ‘M’ is the number of rows and the number of columns such that M must be greater than 2 and less than 20.

Allow the user to input integers into this matrix. Display appropriate error message for an invalid input.

Perform the following tasks: a) Display the input matrix. b) Create a mirror image of the inputted matrix. c) Display the mirror image matrix.

Test your program for the following data and some random data:

Example 1

INPUT:  M = 3
        4   16   12
        8    2   14
        6    1    3
OUTPUT: ORIGINAL MATRIX
        4   16   12
        8    2   14
        6    1    3
        MIRROR IMAGE MATRIX
        12   16   4
        14    2   8
        3    1   6
Example 2
INPUT    :   M = 22
OUTPUT   :   SIZE OUT OF RANGE

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

ISC2013Q2.java Download GitHub
import java.util.*;
class Matrix{
    private int A[][];
    private int M;
    public Matrix(int M){
        this.M = M;
        A=new int[M][M];
    }
    public void getInput(){
        Scanner in = new Scanner( System.in);
        for( int row=0; row<M;row++){
            for( int col=0; col<M;col++){
                A[row][col]=in.nextInt();
            }
        }
        in.close();
    }
    public String toString(){
        String output="";
        for( int row=0; row<M;row++){
            output+="\t";
            for( int col=0; col<M;col++){
                output+=A[row][col];
                if(col<M-1) output+="\t";
            }
            output+="\n";
        }
        return output;
    }
    public void mirror(){
        int temp;
        for( int row=0; row<M;row++){
            for(int col=0; col<M/2; col++){
                temp=A[row][col];
                A[row][col]=A[row][M-1-col];
                A[row][M-1-col]=temp;
            }
        }
    }
}
public class ISC2013Q2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("INPUT:\tM = ");
        int M = in.nextInt();
        if (M > 2 && M < 20) {
            Matrix objMatrix = new Matrix(M);
            objMatrix.getInput();
            System.out.println("OUTPUT:\tORIGINAL MATRIX");
            System.out.println(objMatrix);
            objMatrix.mirror();
            System.out.println("\tMIRROR IMAGE MATRIX");
            System.out.println(objMatrix);
        } else {
            System.out.println("OUTPUT:\tSIZE OUT 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.

Get the free PDF ₹0 ISC 2027 mock papers