ISC 2013 [Question 2] Practical Paper Solved (Mirror Image Matrix)

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 matrix.
(c) Display the mirror image matrix.

Test your program with the sample data and some random data:

Example 1

INPUT : M = 3
4       16      12
8        2       14
4        1        3

OUTPUT :

ORIGINAL MATRIX

4       16       12
8         2       14
4         1        3

MIRROR IMAGE MATRIX

12      16      4
14       2      8
3         1      6

Example 2

INPUT : M = 22

OUTPUT : SIZE OUT OF RANGE

Programming Code:

/**
* The class ImageMatrix_ISC2013 inputs a square matrix and finds its image matrix 
* and finally prints both of them
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Practical 2013 Question 2
*/

import java.io.*;
class ImageMatrix_ISC2013
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the size of the square matrix : ");
int m=Integer.parseInt(br.readLine());

if(m>2 && m<20) //checking given condition
{
int A[][]=new int[m][m];
System.out.println("Enter the elements of the Matrix : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
A[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("*********************");
System.out.println("The original matrix:");
System.out.println("*********************");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"t");
}
System.out.println();
}

// creating the Image Matrix
int B[][]=new int[m][m];
for(int i=0;i<m;i++)
{
int k=0;
for(int j=m-1;j>=0;j--)
{
B[i][k]=A[i][j];
k++;
}
}

//Printing both the Matrix
System.out.println("*********************");
System.out.println("The Mirror Image:");
System.out.println("*********************");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(B[i][j]+"t");
}
System.out.println();
}
}
else
System.out.println("Output : Size Out Of Range");
}
}

Output:

Enter the size of the square matrix : 4
Enter the elements of the Matrix:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
*********************
The original matrix:
*********************
1        2      3      4
5        6      7      8
9     10     11     12
13   14     15     16
*********************
The Mirror Image:
*********************
4       3       2      1
8       7       6      5
12   11      10     9
16   15      14    13

A teacher whose aim is to make Java a fun to learn experience for the school students by striving to build clear fundamentals of every concepts and helping students to develop their ability to reason and logically analyze any given programs.

  • sir can u please explain why are we using variable k while generating mirror image of matrix?

    • variable ‘k’ is being used to refer to the column part of the new mirror image matrix ‘B’ which should start from 0.
      We have to store the last element of the first row of original matrix into as the first element in the new mirror image matrix.
      So, variable ‘j’ is referring to the column part of original matrix going in reverse order from ‘m-1′ to 0. And hence, to refer to the column part of the new mirror image matrix we are using variable ‘k’ going from 0 to ‘m-1′

  • Biprodeep@gmail.com

    Sir, the code you supplied here in the solution of program 2 of ISC 2013(practical) doesn’t tallies with the output you created down….look carefully the case when the user inputs an even order for the mirror matrix ………….you have not handled the different inputs i.e. for odd and for even ……………………likewise I did it accurate……………just glance over the following code…….any try to find the error in your code……..this will benefit many students who learn programming from here…
    Yours Sincerely,
    Biprodeep Roy.
    CORRECTED CODE : –

    /**
    * ISC 2013:Program 2
    *
    * @author (Biprodeep Roy)
    * @version (1.0)
    */
    import java.util.*;
    class MirrorMatrix
    {
    public static void main(String args[])
    {
    Scanner sc=new Scanner(System.in);
    System.out.print(“INPUT:-\nEnter the size of the square matrix : “);
    int m=sc.nextInt();
    if(m>2 && m<20) //checking given condition
    {
    System.out.println("Input Validated:-");
    }
    else
    System.out.println("OUTPUT:-\nSize Out Of Range");
    int A[][]=new int[m][m];
    System.out.println("Enter the elements of the Matrix : ");
    for(int i=0;i<m;i++)
    {
    for(int j=0;j<m;j++)
    {
    A[i][j]=sc.nextInt();
    }
    }
    System.out.println("OUTPUT:-\n*********************");
    System.out.println("The original matrix:");
    System.out.println("*********************");
    for(int i=0;i<m;i++)
    {

    for(int j=0;j<m;j++)
    {

    System.out.print(A[i][j]+"\t");

    }
    System.out.println();
    }
    // creating the Image Matrix
    int B[][]=new int[m][m];
    if(m%2!=0)
    {
    for(int i=0;i=0;j–)
    {

    B[i][k]=A[i][j];

    k++;

    }

    }
    }
    else
    {
    for(int i=0;i=0;j–)
    {
    if(j==((m-2)/2)||j==(m/2))
    B[i][k]=A[i][k];
    else
    B[i][k]=A[i][j];
    k++;
    }
    }
    }
    //Printing both the Matrix.
    System.out.println(“*********************”);

    System.out.println(“The Mirror Image:”);

    System.out.println(“*********************”);
    for(int i=0;i<m;i++)
    {

    for(int j=0;j<m;j++)

    {

    System.out.print(B[i][j]+"\t");

    }

    System.out.println();

    }
    }
    }

    • Hello Biprodeep,
      The code given by us is absolutely fine. There was a typing error in the output generated. You don’t need to handle even and odd condition in this program. Thank you for your concern.
      Have a nice day. :)

  • Damn bst Website,

    Love it.

Your Turn To Talk

Your email address will not be published.