How to print arrays’s mirror image of 2D array (3X3) in C++
In this tutorial, we are going to write a program to enter integers in a 2D array (3X3) and display the array’s mirror image in C++. This program is a very important program as an application of array concept.
It is often asked in interviews and many written examinations and you are supposed to answer the same. If you are searching for the solution of similar program then you are at the right place. I am providing you a very detailed and easy method to do this.
Display or print mirror image of 2D array (3X3) in C++
The mirror image of a 2D array of size 3X3 depends on the position of mirror. In that case, four situations arise in front of us as mirror position on
- Top
- Bottom
- Left side
- Right side of array
In case 1 and 2 the mirror images are like this.
Similarly in case 3 and 4 the mirror images are like this.
So now we can conclude for the images that the top and down as well as left and right mirror images will be the same. So there arise only two cases-
- Top and bottom mirror image.
- Left and right mirror image.
Also read
Remove or eliminate extra spaces from a string in C++
Insert an element in an array in C++
Array’s mirror image for top and bottom mirror position
Methodology
The logic behind the code is entering the array elements by user, then it simply enters the image elements by just exchanging the first row with the third row and vice versa. The second row remains as it is. In the code, we exchange the original array “array” with the image array “arrayimg” by following line of code.
for(int i=0;i<3;i++){ //prepare array's image data for(int j=0;j<3;j++){ arrayimg[i][j]=array[2-i][j]; // logic for array's image } }
Program code
This code is tested and its working fine in CodeBlocks v 16.01.
#include <iostream> #include<string.h> using namespace std; int main() { int array[3][3]; // original array int arrayimg[3][3]; // image array cout<<"Enter 9 array elements "<<endl; for(int i=0;i<3;i++){ // enter original array data for(int j=0;j<3;j++) cin>>array[i][j]; } for(int i=0;i<3;i++){ //prepare array's image data for(int j=0;j<3;j++){ arrayimg[i][j]=array[2-i][j]; // logic for array's image } } cout<<"Original array elements-"<<endl; for(int i=0;i<3;i++){ for(int j=0;j<3;j++) cout<<array[i][j]<<"\t"; //print original array cout<<endl; } cout<<"Array's image for mirror at above and bottom position of array-"<<endl; for(int i=0;i<3;i++){ for(int j=0;j<3;j++) cout<<arrayimg[i][j]<<"\t"; //print image elements cout<<endl; } return 0; }
Output example
Enter 9 array elements 45 14 65 32 41 28 71 69 26 Original array elements- 45 14 65 32 41 28 71 69 26 Array's image for mirror at above and bottom position of array- 71 69 26 32 41 28 45 14 65
Array’s mirror image for left and right mirror position
Methodology
The logic for this program will be same as we have done above. Just a small difference i.e. instead of exchanging rows we will replace first column with the third and again the second i.e. middle column will remain as it is.
This is achieved by following code lines
for(int i=0;i<3;i++){ //prepare array's image data for(int j=0;j<3;j++){ arrayimg[i][j]=array[i][2-j]; // logic for array's image } }
Program code
#include <iostream> #include<string.h> using namespace std; int main() { int array[3][3]; // original array int arrayimg[3][3]; // image array cout<<"Enter 9 array elements "<<endl; for(int i=0;i<3;i++){ // enter original array data for(int j=0;j<3;j++) cin>>array[i][j]; } for(int i=0;i<3;i++){ //prepare array's image data for(int j=0;j<3;j++){ arrayimg[i][j]=array[i][2-j]; // logic for array's image } } cout<<"Original array elements-"<<endl; for(int i=0;i<3;i++){ for(int j=0;j<3;j++) cout<<array[i][j]<<"\t"; //print original array cout<<endl; } cout<<"Array's image for mirror at left and right position of array-"<<endl; for(int i=0;i<3;i++){ for(int j=0;j<3;j++) cout<<arrayimg[i][j]<<"\t"; //print image elements cout<<endl; } return 0; }
Example output
Enter 9 array elements 14 56 29 4 31 47 68 21 16 Original array elements- 14 56 29 4 31 47 68 21 16 Array's image for mirror at left and right position of mirror- 29 56 14 47 31 4 16 21 68
Hope you are now able to understand the logic behind these programs.
Leave a Reply