-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathMatrixReversal.java
96 lines (91 loc) · 1.98 KB
/
MatrixReversal.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import java.util.Scanner;
class MatrixReversal
{
private int arr[][];
private int m;
private int n;
public MatrixReversal(int mm, int nn)
{
m=mm;
n = nn;
arr=new int[m][n];
}
public void fillArray( )
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter matrix elements::");
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
arr[i][j] = sc.nextInt();
}
}
}
public int reverse(int x)
{
int rev = 0;
for(int i = x; i != 0; i /= 10)
rev = rev * 10 + i % 10;
return rev;
}
public void revMat(MatrixReversal p)
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
this.arr[i] [j] = reverse(p.arr[i] [j]);
}
}
}
public void show()
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
System.out. print(arr[i][j] + "\t");
}
System.out.println();
}
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows::");
int x = sc.nextInt();
System.out.print("Enter number of columns::");
int y = sc.nextInt();
MatrixReversal obj1 = new MatrixReversal(x, y);
MatrixReversal obj2 = new MatrixReversal(x, y);
obj1.fillArray();
obj2.revMat(obj1);
System.out.println("Original Matrix is::");
obj1.show();
System.out.println("Matrix with reversed elements");
obj2.show();
}
}
/* OUTPUT
Enter number of rows::3
Enter number of columns::3
Enter matrix elements::
2
3
4
10
12
23
43
25
21
Original Matrix is::
2 3 4
10 12 23
43 25 21
Matrix with reversed elements
2 3 4
1 21 32
34 52 12
*/