-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathReverseElementColumnwiseInMatrix.java
54 lines (54 loc) · 1.53 KB
/
ReverseElementColumnwiseInMatrix.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
//WAP in java to define a method to reverse the element row wise in the matrix.
import java.util.Scanner;
public class ReverseElementColumnwiseInMatrix{
public static void main(String[] args) {
int mat[][] = readMatrix();
System.out.println("The User Entered Matrix is : ");
displayMatrix(mat);
reverseElementColumnwiseInMatrix(mat);
System.out.println("After reversing rowwise the final matrix is : ");
displayMatrix(mat);
}
public static int[][] readMatrix()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows you want : ");
int r = sc.nextInt();
System.out.println("Enter the number of column you want : ");
int c = sc.nextInt();
int mat[][] = new int[r][c];
System.out.println("Enter the "+r*c+" elements row wise : ");
for(int i=0;i<mat.length;i++)
{
for(int j=0;j<mat[i].length;j++)
{
mat[i][j] = sc.nextInt();
}
}
return mat;
}
public static void displayMatrix(int mat[][])
{
//System.out.println("The User Entered Matrix is : ");
for(int i=0;i<mat.length;i++)
{
for(int j=0;j<mat[i].length;j++)
{
System.out.print(mat[i][j]+" ");
}
System.out.println();
}
}
public static void reverseElementColumnwiseInMatrix(int mat[][])
{
for(int i=0;i<mat.length/2;i++)
{
for(int j=0;j<mat[i].length;j++)
{
int temp = mat[i][j];
mat[i][j] = mat[mat[i].length-i-1][j];
mat[mat[i].length-i-1][j] = temp;
}
}
}
}