|
| 1 | +/* Given a 2D matrix, print all elements of the given matrix in diagonal order. |
| 2 | +For example, consider the following 5 X 4 input matrix. |
| 3 | +Example: |
| 4 | +
|
| 5 | +1 2 3 4 |
| 6 | +5 6 7 8 |
| 7 | +9 10 11 12 |
| 8 | +13 14 15 16 |
| 9 | +17 18 19 20 |
| 10 | +
|
| 11 | +Diagonal printing of the above matrix is |
| 12 | +
|
| 13 | +1 |
| 14 | +5 2 |
| 15 | +9 6 3 |
| 16 | +13 10 7 4 |
| 17 | +17 14 11 8 |
| 18 | +18 15 12 |
| 19 | +19 16 |
| 20 | +20 */ |
| 21 | + |
| 22 | +import java.io.*; |
| 23 | +class diagonal |
| 24 | +{ |
| 25 | + int [][]a; |
| 26 | + int p,q; |
| 27 | + diagonal() // A constructor |
| 28 | + { |
| 29 | + p=0; |
| 30 | + q=0; |
| 31 | + } |
| 32 | + void input ()throws IOException // A method to input the array order and elements. |
| 33 | + { InputStreamReader inp=new InputStreamReader(System.in); |
| 34 | + BufferedReader br=new BufferedReader(inp); |
| 35 | + System.out.println("Enter the matrix order of the matrix"); |
| 36 | + p=Integer.parseInt(br.readLine()); |
| 37 | + q=Integer.parseInt(br.readLine()); |
| 38 | + a=new int[p][q]; // creating a 2D array. |
| 39 | + System.out.println("now enter the matrix elements"); |
| 40 | + for(int i=0;i<p;i++) |
| 41 | + { |
| 42 | + for(int j=0;j<q;j++) |
| 43 | + { a[i][j]=Integer.parseInt(br.readLine()); } |
| 44 | + } |
| 45 | + System.out.println("the matrix elements are"); |
| 46 | + for(int i=0;i<p;i++) |
| 47 | + { |
| 48 | + for(int j=0;j<q;j++) |
| 49 | + { System.out.print(a[i][j]+"\t");} |
| 50 | + System.out.println("\n"); |
| 51 | + } |
| 52 | + } |
| 53 | + void logic() // A method to print the diagonal elements.. |
| 54 | + { |
| 55 | + /* The logic() method prints the elements on the diagonal one by one using their index positions |
| 56 | + Firstly starting with the number in the top most left corner. Then the number below it and the |
| 57 | + number diagonaly oposite to it is printed, thus moving diagonaly from left to right. |
| 58 | + */ |
| 59 | + |
| 60 | + System.out.println("The diagonal elements of the matrix are"); |
| 61 | + for(int i=0;i<p;i++) //This part of the for loop prints diagonal elements.. |
| 62 | + { int j=i; //until the principal diagonal(including elements on principal diagonal.. |
| 63 | + for(int x=0;x<=i && x<q;x++,j--) |
| 64 | + { |
| 65 | + System.out.print(a[j][x]+"\t"); |
| 66 | + } |
| 67 | + System.out.println(); |
| 68 | + } |
| 69 | + for( int i=1;i<q;i++) //This part is for the elements that are below the principal diagonal... |
| 70 | + { |
| 71 | + int j=p-1; |
| 72 | + for(int x=i;x<q;x++,j--) //Doing a dry run of the code with some smaple inputs |
| 73 | + { // is suggested for understanding the logic() method |
| 74 | + System.out.print(a[j][x]+"\t"); |
| 75 | + } |
| 76 | + System.out.println(); |
| 77 | + } |
| 78 | +} |
| 79 | +public static void main(String[] args)throws IOException |
| 80 | + { |
| 81 | + diagonal obj= new diagonal(); |
| 82 | + obj.input(); |
| 83 | + obj.logic(); |
| 84 | + } |
| 85 | +} |
0 commit comments