Mirror Image of Matrix ISC 2013 Practical

Write a program to declare a square matrix a[][] of order (m × 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

import java.io.*;
class Mirror{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("m = ");
        int m = Integer.parseInt(br.readLine());
        if(m < 3 || m > 19){
            System.out.println("SIZE OUT OF RANGE");
            return;
        }
        int a[][] = new int[m][m];
        int b[][] = new int[m][m];
        int p = 0;
        int q = m - 1;
        System.out.println("Enter matrix elements:");
        for(int i = 0; i < m; i++){
            q = m - 1;
            for(int j = 0; j < m; j++){
                a[i][j] = Integer.parseInt(br.readLine());
                b[p][q] = a[i][j];
                q--;
            }
            p++;
        }
        System.out.println("ORIGINAL MATRIX");
        for(int i = 0; i < m; i++){
            for(int j = 0; j < m; j++){
                System.out.print(a[i][j] + "\t");
            }
            System.out.println();
        }
        System.out.println("MIRROR IMAGE MATRIX");
        for(int i = 0; i < m; i++){
            for(int j = 0; j < m; j++){
                System.out.print(b[i][j] + "\t");
            }
            System.out.println();
        }
    }
}

Leave a Reply

%d bloggers like this: