Friday 25 October 2013

Mirror Matrix

/*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*/



import java.io.*;
class MirrorArray
{
int a[][],b[][],m;
void input()throws IOException
{
BufferedReader x=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a no. that is more than 2 and less than 20");
m=Integer.parseInt(x.readLine());
a=new int [m][m];b=new int [m][m];
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print("Enter a number for ("+i+","+j+")\n");
a[i][j]=Integer.parseInt(x.readLine());
}
}
}
void CreateMirrorImage()
{
int i,j,k,l;
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
for(k=0;k<m;k++)
{
if((j+k)==(m-1))
b[i][k]=a[i][j];
}
}
}
}
void ShowImage()
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
System.out.print(b[i][j]+" ");
}
System.out.println();
}
}
void ShowArray()
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
public static void main(String a[])throws IOException
{
MirrorArray call=new MirrorArray();
call.input();
System.out.println("Your Array");
System.out.println("----------");
call.ShowArray();
System.out.println("\nMirror Array");
System.out.println("----------");
call.CreateMirrorImage();
call.ShowImage();
}
}

No comments:

Post a Comment