forked from ossamamehmood/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MirrorMatrix.java
54 lines (40 loc) · 1.47 KB
/
MirrorMatrix.java
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
import java.util.Scanner;
class MirrorMatrix {
public static void main(String[] shit) {
Scanner sc = new Scanner(System.in);
System.out.println("Input matrix's length");
int length = sc.nextInt();
String space = " ";
space = space.repeat(length * 2 - 2);
// Defining an empty matrix / 2d array
int[][] ar = new int[length][length];
int[][] ar2 = new int[length][length];
System.out.println("Input the " + (length * length) + " elements of the matrix.");
// Taking user input for filling the 2d array
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
ar[i][j] = sc.nextInt();
int J = length - j - 1;
int I = length - i - 1;
ar2[I][J] = ar[i][j];
}
}
// Printing the provided matrix
printMatrix(ar, length, space);
// Printing the mirror matrix
printMatrix(ar2, length, space);
sc.close();
}
public static void printMatrix(int[][] ar, int length, String space) {
System.out.println(" _" + space + "_");
for (int i = 0; i < length; i++) {
System.out.print("| ");
for (int j = 0; j < length; j++) {
System.out.print(ar[i][j] + " ");
}
System.out.print("|");
System.out.println();
}
System.out.println(" _" + space + "_");
}
}