Mirror Image Matrix Program in Java and Python
Mirror image matrix program with algorithm, explanation, Java solution and simple Python solution for ISC students.
Question:
Write a program to declare a square matrix A[][] of order M × M, where M must be greater than 2 and less than 20. Allow the user to input integers into the matrix.
Perform the following tasks: display the input matrix, create a mirror image matrix and display the mirror image matrix.
Algorithm:
Step 1: Start.
Step 2: Accept the matrix order M.
Step 3: If M is not greater than 2 and less than 20, display SIZE OUT OF RANGE and stop.
Step 4: Declare square matrix A[M][M].
Step 5: Input all matrix elements using nested row and column loops.
Step 6: Display the original matrix by printing columns from 0 to M - 1 for each row.
Step 7: For the mirror image, keep the row loop unchanged.
Step 8: For each row, run the column loop from M - 1 down to 0 and display A[row][column].
Step 9: Continue until all rows have been printed in reversed column order.
Step 10: Use a row loop from 0 to M - 1 and a column loop from M - 1 down to 0 for the mirror output.
Step 11: Do not alter the original matrix while printing the mirror image; only the display order changes.
Step 12: Initialize row and column index variables through nested loops for original display.
Step 13: Compare no values while producing mirror image; only update the column index in reverse order.
Step 14: Stop.
Explanation:
A mirror image matrix is formed by reversing each row horizontally. The row positions remain the same, but the columns are printed in reverse order.
The program first validates the matrix order because the ISC question restricts M to values greater than 2 and less than 20.
The original matrix is printed using the normal column order from 0 to M - 1. This shows the input exactly as entered.
For the mirror image, the outer loop still moves row by row, but the inner loop starts from the last column and moves backward to column 0. This creates the horizontal reflection.
No extra matrix is required for the mirror image because the original row can be printed in reverse column order. The loop variable for columns moves from M - 1 down to 0, which produces the reflected row directly.
The mirror image matrix is formed by reversing each row horizontally. The row order remains the same, but the columns are read from right to left. The program therefore uses nested loops: the outer loop selects the row, and the inner loop prints or stores elements from the last column down to the first column. This does not require changing the original matrix unless a separate mirrored matrix is needed. The key concept is column reversal within each row, not full matrix rotation.
Java Program:
/**
* The class ImageMatrix_ISC2013 inputs a square matrix and finds its image matrix
* and finally prints both of them
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Practical 2013 Question 2
*/
import java.util.Scanner;
class ImageMatrix_ISC2013
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the square matrix : ");
int m=sc.nextInt();
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]=sc.nextInt();
}
}
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");
}
}Equivalent Python Program:
# Read the matrix or array size and store the values for indexed processing.
# Nested loops are used because each row/column or array position must be checked.
# Print the processed array or matrix in the required output format.
m = int(input("M = "))
if m <= 2 or m >= 20:
print("SIZE OUT OF RANGE")
else:
A = []
print("Enter the elements:")
for i in range(m):
row = []
for j in range(m):
row.append(int(input()))
A.append(row)
print("ORIGINAL MATRIX")
for i in range(m):
for j in range(m):
print(A[i][j], end=" ")
print()
print("MIRROR IMAGE MATRIX")
for i in range(m):
for j in range(m - 1, -1, -1):
print(A[i][j], end=" ")
print()Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.