Java Program to Print Boundary Elements of a Matrix



In this article, we will understand how to print boundary elements of a matrix. A matrix is representation of the elements in rows and columns. Boundary elements are those elements which are not surrounded by elements on all four directions. For example, the elements in the first row, first column, last row and last column.

Below is a demonstration of the same −

Suppose our input is

The input matrix:
9 8 9 8
8 7 8 7
7 6 7 6
6 5 6 5

The desired output would be

The border elements of the matrix is:
9 8 9 8
8     7
7     6
6 5 6 5

Algorithm

Step 1 - START
Step 2 - Declare an integer matrix namely input_matrix, an object of the class BoundaryElements namely border_values.
Step 3 - Define the values.
Step 4 - Iterate over each element of the matrix using two for-loops and check if the element is a boundary element using Boolean OR condition.
Step 5 - Display the boundary elements.
Step 5 - Stop

Example 1

Here, the input is being entered by the user based on a prompt.

public class BoundaryElements {
   public static void main(String[] args) {
      int input_matrix[][] = new int[][] {
         { 9, 8, 9, 8 },
         { 8, 7, 8, 7 },
         { 7, 6, 7, 6 },
         { 6, 5, 6, 5 }
      };
      System.out.println("The matrix is defined as: ");
      for (int x = 0; x < input_matrix.length; x++) {
         for (int y = 0; y < input_matrix[x].length; y++) {
            System.out.print(input_matrix[x][y] + " ");
         }
         System.out.println();
      }
      BoundaryElements border_values = new BoundaryElements();
      System.out.println("The border elements of the matrix is:");
      for (int x = 0; x < input_matrix.length; x++) {
         for (int y = 0; y < input_matrix[x].length; y++) {
            if (x == 0 || y == 0 || x == input_matrix.length - 1
               || y == input_matrix[x].length - 1) {
               System.out.print(input_matrix[x][y] + " ");
            } else {
               System.out.print(" ");
            }
         }
         System.out.println();
      }
   }
}

Output

The matrix is defined as:
9 8 9 8
8 7 8 7
7 6 7 6
6 5 6 5
The border elements of the matrix is:
9 8 9 8
8   7
7   6
6 5 6 5

Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class BoundryElements {
   public void Boundary_Elements(int input_matrix[][]) {
      System.out.println("The matrix is defined as: ");
      for (int x = 0; x < input_matrix.length; x++) {
         for (int y = 0; y < input_matrix[x].length; y++) {
            System.out.print(input_matrix[x][y] + " ");
         }
         System.out.println();
      }
      System.out.println("The border elements of the matrix is:");
      for (int x = 0; x < input_matrix.length; x++) {
         for (int y = 0; y < input_matrix[x].length; y++) {
            if (x == 0 || y == 0 || x == input_matrix.length - 1
               || y == input_matrix[x].length - 1) {
               System.out.print(input_matrix[x][y] + " ");
            } else {
               System.out.print(" ");
            }
         }
         System.out.println();
      }
   }
   public static void main(String[] args) {
      int input_matrix[][] = new int[][] {
         { 9, 8, 9, 8 },
         { 8, 7, 8, 7 },
         { 7, 6, 7, 6 },
         { 6, 5, 6, 5 }
      };
      BoundryElements border_values = new BoundryElements();
      border_values.Boundary_Elements(input_matrix);
   }
}

Output

The matrix is defined as:
9 8 9 8
8 7 8 7
7 6 7 6
6 5 6 5
The border elements of the matrix is:
9 8 9 8
8   7
7   6
6 5 6 5

Advertisements