Interchange First and Last Elements in a Matrix Across Columns



In this article, we will understand how to interchange elements of first and last in a matrix across columns in Java. The matrix has a row and column arrangement of its elements. A matrix with m rows and n columns can be called as m × n matrix.

Individual entries in the matrix are called elements and can be represented by a[i][j] which suggests that the element a is present in the ith row and jth column.

Problem Statement

Write a Java program to interchange elements of first and last in a matrix across columns.

Input 

The matrix is defined as:
4 5 6 7
1 7 3 4
11 12 13 14
23 24 25 50

Output

The matrix after swapping the elements:
7 5 6 4
4 7 3 1
14 12 13 11
50 24 25 23

Approaches to interchange elements of first and last in matrix

Below are the approaches to interchange elements of first and last in the matrix ?

Using main() method

Following are the steps to interchange elements of first and last in the matrix ?

  • Define the 2D matrix in the main method with predefined values.
  • Print the matrix to display its original state.
  • Get the number of rows using input_matrix.length.
  • Loop through each row using for loop to swap the first and last elements using a temporary variable.
  • Swap the element in the first column with the element in the last column for each row.
  • Print the matrix after the swapping process.

Example

Here, we bind all the operations together using the main method ?

public class MatrixSwap {
   static void swap(int input_matrix[][]) {
   }
   public static void main(String args[]) {
      int input_matrix[][] = {
         { 4, 5, 6, 7 },
         { 1, 7, 3, 4 },
         { 11, 12, 13, 14 },
         { 23, 24, 25, 50 }
      };
      System.out.println("The matrix is defined as: ");
      for (int i = 0; i < input_matrix.length; i++) {
         for (int j = 0; j < input_matrix[0].length; j++)
            System.out.print(input_matrix[i][j] + " ");
         System.out.println();
      }
      int matrix_length = input_matrix.length;
      for (int i = 0; i < input_matrix[0].length; i++) {
         int temp = input_matrix[i][0];
         input_matrix[i][0] = input_matrix[i][matrix_length - 1];
         input_matrix[i][matrix_length - 1] = temp;
      }
      System.out.println("\nThe matrix after swapping the elements: ");
      for (int i = 0; i < matrix_length; i++) {
         for (int j = 0; j < input_matrix[0].length; j++)
            System.out.print(input_matrix[i][j] + " ");
         System.out.println();
      }
   }
}

Output

The matrix is defined as:
4 5 6 7
1 7 3 4
11 12 13 14
23 24 25 50

The matrix after swapping the elements:
7 5 6 4
4 7 3 1
14 12 13 11
50 24 25 23

Using encapsulation

Following are the steps to interchange elements of first and last in the matrix ?

  • Define the matrix in the main method with predefined values.
  • Print the matrix to display its original state.
  • Create a separate swap function to handle the swapping logic.
  • In the swap function, get the number of rows and loop through each row using for loop to swap the first and last elements using a temporary variable.
  • Call the swap function from the main method.
  • Print the matrix after the swapping process inside the swap function.

Example

Here, we encapsulate the operations into functions exhibiting object-oriented programming ?

public class MatrixSwap {
   static void swap(int input_matrix[][]) {
      int matrix_length = input_matrix.length;
      for (int i = 0; i < input_matrix[0].length; i++) {
         int temp = input_matrix[i][0];
         input_matrix[i][0] = input_matrix[i][matrix_length - 1];
         input_matrix[i][matrix_length - 1] = temp;
      }
      System.out.println("\nThe matrix after swapping the elements: ");
      for (int i = 0; i < matrix_length; i++) {
         for (int j = 0; j < input_matrix[0].length; j++)
            System.out.print(input_matrix[i][j] + " ");
         System.out.println();
      }
   }
   public static void main(String args[]) {
      int input_matrix[][] = {
         { 4, 5, 6, 7 },
         { 1, 7, 3, 4 },
         { 11, 12, 13, 14 },
         { 23, 24, 25, 50 }
      };
      System.out.println("The matrix is defined as: ");
      for (int i = 0; i < input_matrix.length; i++) {
         for (int j = 0; j < input_matrix[0].length; j++)
            System.out.print(input_matrix[i][j] + " ");
         System.out.println();
      }
      swap(input_matrix);
   }
}

Output

The matrix is defined as:
4 5 6 7
1 7 3 4
11 12 13 14
23 24 25 50

The matrix after swapping the elements:
7 5 6 4
4 7 3 1
14 12 13 11
50 24 25 23
Updated on: 2024-10-10T11:35:44+05:30

697 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements