Mirror Image of a Matrix
Mirror Image of a Matrix in JAVA
Finding Mirror image of a matrix of order M X N is given.
Logic and Program :-
Logic :-
Program :-
import java.io.*;
import java.util.*;
class Mirror
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int m,n,i,j,k,temp;
System.out.println("Enter Array Capacity");
m=in.nextInt(); // Storing capacity of Rows
n=in.nextInt(); // Storing capacity of Columns
int ar[][]=new int[m][n]; // For Original Matrix
int br[][]=new int[m][n]; // For Mirror Matrix
k=n-1; // variable for swapping
System.out.println("Enter Array Elements");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
ar[i][j]=in.nextInt(); // Entering Matrix Elements
}
}
System.out.println("Original Matrix");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(ar[i][j]+"\t"); // Displaying Original Matrix
}
System.out.println();
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
br[i][j]=ar[i][k-j]; // Swapping the Matrix Elements
}
}
System.out.println("Mirror Matrix");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(br[i][j]+"\t"); // Displaying Mirror Matrix
}
System.out.println();
}
} // end of main
} // end of class
import java.util.*;
class Mirror
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int m,n,i,j,k,temp;
System.out.println("Enter Array Capacity");
m=in.nextInt(); // Storing capacity of Rows
n=in.nextInt(); // Storing capacity of Columns
int ar[][]=new int[m][n]; // For Original Matrix
int br[][]=new int[m][n]; // For Mirror Matrix
k=n-1; // variable for swapping
System.out.println("Enter Array Elements");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
ar[i][j]=in.nextInt(); // Entering Matrix Elements
}
}
System.out.println("Original Matrix");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(ar[i][j]+"\t"); // Displaying Original Matrix
}
System.out.println();
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
br[i][j]=ar[i][k-j]; // Swapping the Matrix Elements
}
}
System.out.println("Mirror Matrix");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(br[i][j]+"\t"); // Displaying Mirror Matrix
}
System.out.println();
}
} // end of main
} // end of class
All the best :-)
Comments
Post a Comment