Java Program to Interchange Elements of First and Last in a Matrix Across Rows


In this article, we will understand how to interchange elements of first and last in a matrix across rows. 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 element and can be represented by a[i][j] which suggests that the element a is present in the ith row and jth column.

Below is a demonstration of the same −

Suppose our input is

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

The desired output would be

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

Algorithm

Step 1 - START
Step 2 - Declare an integer matrix namely input_matrix, and an integer value namely matrix_length.
Step 3 - Define the values.
Step 4 - Iterate over each element of the matrix using multiple for-loops and swap the required elements of the matrix using a temporary variable.
Step 5 - Display the result
Step 5 - Stop

Example 1

Here, we bind all the operations together under the ‘main’ function.

public class MatrixSwap {
   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[0][i];
         input_matrix[0][i] = input_matrix[matrix_length - 1][i];
         input_matrix[matrix_length - 1][i] = 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:
23 24 25 50
1 7 3 4
11 12 13 14
4 5 6 7

Example 2

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[0][i];
         input_matrix[0][i] = input_matrix[matrix_length - 1][i];
         input_matrix[matrix_length - 1][i] = 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:
23 24 25 50
1 7 3 4
11 12 13 14
4 5 6 7

Updated on: 29-Mar-2022

106 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements