Mirror image of an array

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```

The above problem was asked in the ISC Computer Science Practical in the year 2013. The program for the above problem is as follows:

```import java.util.*;
class Matrix{
public static int[][] mirrorImage(int A[][]){
int temp, half=A.length/2;
for(int row=0; row < A.length; row++){
for(int col=0; col <=half; col++){
temp=A[row][A.length-1-col];
A[row][A.length-1-col]=A[row][col];
A[row][col]=temp;
}
}
return A;
}
public static void display(int A[][]){
for(int row=0;row<A.length;row++){
System.out.print("\t\t\t");
for(int col=0; col < A.length; col++){
System.out.print(A[row][col]+"\t");
}
System.out.println();
}
}
public static void main( String args[] ){
Scanner sc = new Scanner(System.in);
System.out.print("INPUT:\t\tM=");
int M = sc.nextInt();
if(M<=2 || M>=20){
System.out.println("OUTPUT:\t\tSIZE OUT OF RANGE");
}else{
int A[][] = new int[M][M];
for(int row=0;row < M;row++){
for(int col=0; col<M; col++){
A[row][col]=sc.nextInt();
}
}
System.out.println("OUTPUT:\t\t");
System.out.println("ORIGINAL MATRIX");
display(A);
A=mirrorImage(A);
System.out.println("MIRROR IMAGE MATRIX");
display(A);
}
}
}```

The above code is self-explanatory, the students are advised to pay attention to the function declaration of mirrorImage(). This function accepts a double dimension integer array as an argument and returns an array after storing it’s mirror image in the array. It’s always considered a good practice to break up your programs into functions such that each functions performs a logical task. In the above program the relevant logical tasks are finding the mirror image, displaying the array and the main functions is used for entering the data and passing it to the mirrorImage function to find it’s mirror image. Error handling is also done in the main() function. Also note the use of the escape sequence (\t) to introduce white spaces in an elgant fashion instead of the space character.