import java.util.*;
public class ISC2015Q1 {
public static void main(String[] args) {
int M,N;
System.out.println("Input:");
Scanner in = new
Scanner(System.in);
do {
System.out.println("ENTER ROW SIZE:");
M = in.nextInt();
System.out.println("ENTER COLUMN SIZE:");
N = in.nextInt();
if(M<0 || N < 0)
System.out.println("OUT OF RANGE, TRY AGAIN:");
}while(M<0 || N < 0);
MatRev obj = new MatRev(M,N);
MatRev P = new MatRev(M,N);
obj.fillarray();
System.out.println("Entered Matrix");
obj.show();
obj.revMat(P);
System.out.println("Reversed Matrix");
P.show();
in.close();
}
}
class MatRev {
public void revMat(MatRev P) {
for(int p=0;p<M;p++)
for(int q=0;q<N;q++)
P.mat[p][q]= reverse(mat[p][q]); // storing reverse matrix in the current object P
}
private int reverse (int x) {
int rev = 0,length;
length = Integer.toString(x).length()-1; //
calculates the length of the number x
do { //
if you don't want to use library function
rev += (x%10)*(int)Math.pow(10,length); // use a do while loop
length--;
x /=10;
}while(x > 0);
return rev;
}
public void show() {
for(int p=0;p<M;p++)
for(int q=0;q<N;q++)
if(MAX_MATRIX
< mat[p][q])
MAX_MATRIX
= mat[p][q];
String s,element;
s=Integer.toString(MAX_MATRIX);
for(int i=0;i<M;i++) {
for(int j=0;j<N;j++) {
element=Integer.toString(mat[i][j]); // for formatted output
int tmp=element.length();
while(tmp !=s.length()) {
element +=" ";
tmp++;
}
System.out.print(element+" ");
}
System.out.println();
}
}
public void fillarray() {
Scanner in = new
Scanner(System.in);
System.out.println("Enter the values of the matrix:");
for(int i=0;i<M;i++) {
System.out.println("Enter the elements of row :"+(i+1));
for(int j=0;j<N;j++) {
mat[i][j]=in.nextInt();
}
}
in.close();
}
MatRev(int M, int N){
this.M = M;
this.N = N;
mat = new int[M][N];
mat = new int[M][N];
MAX_MATRIX =0;
}
private int[][] mat;
private int M,N;
private int MAX_MATRIX;
}
No comments:
Post a Comment