Rotate matrix /2-D array 90 degrre left and Right

Write a java program to Rotate matrix 90 degree left and rotate 90 degree right : 

import java.util.Random;
import java.util.Scanner;
public class RotateMatrix {
public static void main(String[] args) {
System.out.println("Enter matrix : ");
int arr[][]=readmatrix();
System.out.println("Matrix is : ");
dispMat(arr);
int arr2[][]=rotate90Left(arr);
System.out.println("90 Degree left rotate matrix is :");
dispMat(arr2);
System.out.println("90 degree right rotate Matrix is :");
arr=rotate90Right(arr);
dispMat(arr);

}
static int[][] rotate90Left(int mat[][]){
ColumnWiseReverse cw=new ColumnWiseReverse();
mat=transposeMat(mat);
cw.colWiseReverse(mat);
return mat;
}
static int[][] rotate90Right(int mat[][]){
RowWiseReverse rw=new RowWiseReverse();
mat=transposeMat(mat);
rw.rowWiseReverse(mat);
return mat;
}
static int[][] transposeMat(int m[][]){
int transmat[][]=new int[m[0].length][m.length];
for(int i=0;i<m.length;i++) {
for(int j=0;j<m[i].length;j++) {
transmat[j][i]=m[i][j];
}
} return transmat;
}
static int[][] readmatrix(){
Random rx=new Random();
Scanner sc=new Scanner(System.in);
System.out.println("Enter Row size and column size :");
int row=sc.nextInt();
int col=sc.nextInt();
int mat[][]=new int[row][col];
System.out.println("Enter Elements in Table : ");
for(int i=0;i<row;i++) {
for(int j=0;j<col;j++) {
mat[i][j]=rx.nextInt(25);
}
sc.close();
}return mat;
}static void dispMat(int mat[][]) {
  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();
}
  }
}

Comments

Popular posts from this blog