-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMirrorMatrix.java
More file actions
58 lines (47 loc) · 1.7 KB
/
Copy pathMirrorMatrix.java
File metadata and controls
58 lines (47 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.Scanner;
public class MirrorMatrix {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input the size of the matrix
System.out.println("Enter the number of rows:");
int rows = scanner.nextInt();
System.out.println("Enter the number of columns:");
int cols = scanner.nextInt();
// Input the matrix
int[][] matrix = new int[rows][cols];
System.out.println("Enter the elements of the matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = scanner.nextInt();
}
}
System.out.println("\nOriginal Matrix:");
printMatrix(matrix);
// Generate the mirror matrix
int[][] mirroredMatrix = mirrorMatrix(matrix);
System.out.println("\nMirror Matrix:");
printMatrix(mirroredMatrix);
scanner.close();
}
// Method to generate the mirror image of a matrix
public static int[][] mirrorMatrix(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
int[][] mirrored = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
mirrored[i][j] = matrix[i][cols - 1 - j];
}
}
return mirrored;
}
// Method to print a matrix
public static void printMatrix(int[][] matrix) {
for (int[] row : matrix) {
for (int element : row) {
System.out.print(element + " ");
}
System.out.println();
}
}
}