Boundary Traversal of Matrix
This program prints boundary traversal of a matrix.
Problem Statement
Write a Java program to print boundary traversal of a 4x4 matrix.
Source Code
| 1 | public class MatrixBoundaryTraversal { |
| 2 | public static void main(String[] args) { |
| 3 | System.out.println("Eduinq Matrix Boundary Traversal"); |
| 4 | int[][] A = { |
| 5 | {1,2,3,4}, |
| 6 | {5,6,7,8}, |
| 7 | {9,10,11,12}, |
| 8 | {13,14,15,16} |
| 9 | }; |
| 10 | for(int j=0;j<4;j++) System.out.print(A[0][j]+" "); |
| 11 | for(int i=1;i<4;i++) System.out.print(A[i][3]+" "); |
| 12 | for(int j=2;j>=0;j--) System.out.print(A[3][j]+" "); |
| 13 | for(int i=2;i>0;i--) System.out.print(A[i][0]+" "); |
| 14 | } |
| 15 | } |
Program Output
Eduinq Matrix Boundary Traversal 1 2 3 4 8 12 16 15 14 13 9 5
Explanation
Boundary traversal prints the first row, then the last column, then the last row in reverse, then the first column in reverse, with separate loops handling each side, demonstrating a full boundary traversal.