Mirror Image Meaning

Mirror image or mirror inverse of a 2d array is another array whose columns are interchanged, meaning elements of first column become the elements of last column, elements of second column become the elements of second last column and so on.
It is just like an array put before a mirror. Image below will clarify a mirror image of an array or a matrix.

Only columns of a mirror image are interchanged. Rows remain unaffected.

How to create mirror image of 2d array

Following program creates a mirror image an array.
Array size and elements are taken as an input from the user.

import java.util.Scanner;

public class ArrayImageCreator {
  public static void main(String[] args) {
    processImageArray();
  }

  static void processImageArray() {
    // initialize reader
    Scanner reader = new Scanner(System.in);
    System.out.println("Enter the number of rows");
    // read the number of rows in array
    int rowCount = reader.nextInt();
    System.out.println("Enter number of columns");
    // read the number of columns in array
    int columnCount = reader.nextInt();
    // create array
    int[][] array = new int[rowCount][columnCount];
    System.out.println("Starting input of array elements...\n");
    // loop to read array elements
    for (int i = 0; i < rowCount; i++) {
      for (int j = 0; j < columnCount; j++) {
        System.out.println("Enter element of row " + (i + 1) + 
                                ", column " + (j + 1));
 // assign element to array
 array[i][j] = reader.nextInt();
      }
    }
    // close reader
    reader.close();
    System.out.println("Entered array is : ");
    // display array elements
    displayArray(array);
    // create a new array of same size as original array
    int[][] mirrorImage = new int[rowCount][columnCount];
    // iterate over the rows of actual array
    for (int row = 0; row < rowCount; row++) { 
      // initialize column for image array 
      int imageColumn = 0; 
      // iterate over the columns of original array in reverse direction
      for (int column = columnCount - 1; column >= 0; column--) {
        // get element at current row and column
 int element = array[row][column];
 // assign element to the image array
 mirrorImage[row][imageColumn] = element;
 // increment the image array column counter
 imageColumn++;
      }
    }
    System.out.println("Mirror image of array is : ");
    // display image array
    displayArray(mirrorImage);
  }

  /**
   * Displays the contents of the array received as parameter
   * @param array
   */
  static void displayArray(int[][] array) {
    for (int i = 0; i < array.length; i++) {
      int[] row = array[i];
      for (int j = 0; j < row.length; j++) {
        int element = array[i][j];
        System.out.print(element + " ");
      }
      System.out.println();
    }
  }
}

Output

Enter the number of rows
3
Enter number of columns
3
Starting input of array elements…

Enter element of row 1, column 1
2
Enter element of row 1, column 2
3
Enter element of row 1, column 3
5
Enter element of row 2, column 1
7
Enter element of row 2, column 2
9
Enter element of row 2, column 3
20
Enter element of row 3, column 1
35
Enter element of row 3, column 2
1
Enter element of row 3, column 3
12
Entered array is :
2 3 5
7 9 20
35 1 12
Mirror image of array is :
5 3 2
20 9 7
12 1 35

Explanation

The program first reads the total row and column count in the original array as user input. Based on these, the actual and image arrays are created.
It then asks the user to input the array elements in a series of iterations. Finally, it converts the actual array to its mirror image.
For conversion of the array to its mirror image, the program creates 2 loops, one nested inside another.
Outer loop iterates over the rows of the array while inner loop iterates over the columns of the array in reverse order, that is, starting from the total column count to 0.
A separate variable imageColumn is used to keep track of the column of mirror image array. This variable is incremented in every iteration of inner loop.
Now, at the first iteration of outer and inner loop, the current row is 0 and current column is the last column of the actual array, this element is assigned to row 0 and column 0 of the image array and so on…
Following table illustrates the values of different counters in every iteration of outer and inner loops.

Outer loop iterationInner loop iterationActual/Mirror array rowActual array ColumnMirror array Column
11020
12011
13002
21120
22111
23102
31220
32211
33202

Note that these values are for an array with 3 rows and 3 columns. Also, the row and column count starts at 0 and ends at max(row or column) – 1.
Also note that only the column values of mirror array change, row values remain the same.
Hope the article was useful.

Leave a Reply