Matrix Mirror Image ISC Computer Science 2013 Practical
Write a program to declare a square matrix a[][] of order (M × 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 1INPUT: 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 2INPUT: M = 22
OUTPUT: SIZE OUT OF RANGE
import java.util.Scanner;
class Mirror{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("M = ");
int m = Integer.parseInt(in.nextLine());
if(m < 3 || m > 19){
System.out.println("SIZE OUT OF RANGE");
return;
}
int a[][] = new int[m][m];
System.out.println("Enter matrix elements:");
for(int i = 0; i < m; i++){
for(int j = 0; j < m; j++){
a[i][j] = Integer.parseInt(in.nextLine());
}
}
System.out.println("ORIGINAL MATRIX");
for(int i = 0; i < m; i++){
for(int j = 0; j < m; j++)
System.out.print(a[i][j] + "\t");
System.out.println();
}
for(int i = 0; i < m; i++){
for(int j = 0; j < m / 2; j++){
int temp = a[i][j];
a[i][j] = a[i][m - 1 - j];
a[i][m - 1 - j] = temp;
}
}
System.out.println("MIRROR IMAGE MATRIX");
for(int i = 0; i < m; i++){
for(int j = 0; j < m; j++)
System.out.print(a[i][j] + "\t");
System.out.println();
}
}
}
Comments
Post a Comment