Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
Problem
2.1.
Example
3.
Algorithm
3.1.
C++ Code
3.1.1.
Output
3.2.
Java Code
3.2.1.
Output
3.3.
Python Code
3.3.1.
Output
3.4.
PHP Code
3.5.
Output
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Interchange elements of first and last rows in matrix

Author SHIVANGI MALL
0 upvote

Introduction 

In this article, we will learn how to interchange the elements of the first and last row in a matrix. The elements of the matrix are arranged in a row and column format. A m X n matrix refers to a matrix with m rows and n columns.

Individual entries in the matrix are referred to as elements, and they are represented by the symbol a[i][j], which indicates that element a is found in the ith row and jth column.

Problem

Given a 4 x 4 matrix, we must swap the first and last row elements and display the new matrix.

Example

Input : 9  5  5  2
        2  6  1  2
        2  8  1  2
        2  4  1  2
        
After Interchanging the elements of first and last rows in the input matrix:

Output : 2  4  1  2
         2  6  1  2
         2  8  1  2
         9  5  5  2
         

Input : 4  7  5  2
        2  3  4  1
        5  6  6  5
        7  2  3  1
        
After Interchanging the elements of first and last rows in the input matrix:        

Output : 7  2  3  1
         2  3  4  1
         5  6  6  5
         4  7  5  2

Algorithm

Step 1 - START
Step 2 - Declare an integer matrix namely matrix and an integer value namely n.
Step 3 - Define the values.
Step 4 - Run one for loop from i=0 to i<n and swap matrix[0][i] with matrix[n-1][i] using a temporary variable.
Step 5 - Display the result
Step 5 - Stop

C++ Code

// C++ code to swap the element of first
// and last row and display the result
#include <iostream>
using namespace std;

#define n 4
void interchange(int m[][n])
{
int rows = n;

// swapping of element between first
// and last rows
for (int i = 0; i < n; i++)
{
int t = m[0][i];
m[0][i] = m[rows - 1][i];
m[rows - 1][i] = t;
}
}


// Driver function
int main()
{
// input in the array
int matrix[n][n] = { { 11, 0, 1, 6 },
{ 4, 7, 6, 5 },
{ 3, 2, 1, 8 },
{ 5, 4, 0, 7 } };


cout<<"The matrix is defined as:"<<endl;
for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
        cout<<matrix[i][j]<<" ";
    }
    cout<<endl;
}
interchange(matrix);
cout<<endl;
cout<<"The matrix after swapping the elements:"<<endl;
// printing the interchanged matrix
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
cout << matrix[i][j] << " ";
cout << endl;
}
}
You can also try this code with Online C++ Compiler
Run Code

Output

The matrix is defined as:
11 0 1 6 
4 7 6 5 
3 2 1 8 
5 4 0 7 

The matrix after swapping the elements:
5 4 0 7 
4 7 6 5 
3 2 1 8 
11 0 1 6 
You can also try this code with Online C++ Compiler
Run Code

Java Code

// Java code to swap the element of first
// and last row and display the result
import java.io.*;
 
public class Interchange {
     
    static void interchangeElement(int matrix[][])
    {
        int rows = matrix.length;
         
        // swapping of element between first
        // and last rows
        for (int i = 0; i < matrix[0].length; i++) {
            int t = matrix[0][i];
            matrix[0][i] = matrix[rows-1][i];
            matrix[rows-1][i] = t;
        }
    }
     
    // Driver code
    public static void main(String args[]) throws IOException
    {
        // input in the array
        int matrix[][] = { { 11, 0, 1, 6 },
{ 4, 7, 6, 5 },
{ 3, 2, 1, 8 },
{ 5, 4, 0, 7 } };
        System.out.print("The matrix is defined as:");    
        System.out.println();        
        interchangeElement(matrix);
        System.out.println();
        System.out.print("The matrix after swapping the elements:"); 
        System.out.println();
        // printing the interchanged matrix
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++)
                System.out.print(matrix[i][j] + " ");
            System.out.println();
        }
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

The matrix is defined as:
11 0 1 6 
4 7 6 5 
3 2 1 8 
5 4 0 7 

The matrix after swapping the elements:
5 4 0 7 
4 7 6 5 
3 2 1 8 
11 0 1 6 
You can also try this code with Online C++ Compiler
Run Code

Python Code

# Python code to swap the element
# of first and last row and display
# the result

def interchangeFirstLast(matrix, n, m):
rows = n

# swapping of element between
# first and last rows
for i in range(n):
t = matrix[0][i]
matrix[0][i] = matrix[rows-1][i]
matrix[rows-1][i] = t


# Driver Program
matrix = [[11, 0, 1, 6],
[4, 7, 6, 5],
[3, 2, 1, 8],
[5, 4, 0, 7]]


n = 4
m = 4
print("The matrix is defined as:\n")
for i in range(n):
for j in range(m):
print(matrix[i][j], end = " ")
print("\n")
interchangeFirstLast(matrix, n, m)
print("The matrix after swapping the elements:\n")


# printing the interchanged matrix
for i in range(n):
for j in range(m):
print(matrix[i][j], end = " ")
print("\n")
You can also try this code with Online Python Compiler
Run Code

Output

The matrix is defined as:
11 0 1 6 

4 7 6 5 

3 2 1 8
 
5 4 0 7 

The matrix after swapping the elements:

5 4 0 7 

4 7 6 5 

3 2 1 8 

11 0 1 6 

PHP Code

<?php
// PHP code to swap the element of first
// and last row and display the result
$n = 4;
 
function interchangElement(&$matrix)
{
        global $n;
        $rows = $n;
          
        // swapping of element between first
        // and last rows
        for ($i = 0; $i < $n; $i++)
        {
            $t = $matrix[0][$i];
            $matrix[0][$i] = $matrix[$rows - 1][$i];
            $matrix[$rows - 1][$i] = $t;
        }
}
  
// Driver function
 
// input in the array
$matrix = array(array(11, 0, 1, 6),
            array(4, 7, 6, 5),
            array( 3, 2, 1, 8),
            array(5, 4, 0, 7));
echo "The matrix is defined as:\n";    
for ($i = 0; $i < $n; $i++)
{
    for ($j = 0; $j < $n; $j++)
        echo $matrix[$i][$j] . " ";
    echo "\n";
}
interchangElement($matrix);
  echo "\n";
echo "The matrix after swapping the elements:\n";  
// printing the interchanged matrix
for ($i = 0; $i < $n; $i++)
{
    for ($j = 0; $j < $n; $j++)
        echo $matrix[$i][$j] . " ";
    echo "\n";
}
?>
You can also try this code with Online PHP Compiler
Run Code

Output

The matrix is defined as:
11 0 1 6 
4 7 6 5 
3 2 1 8 
5 4 0 7 

The matrix after swapping the elements:
5 4 0 7 
4 7 6 5 
3 2 1 8 
11 0 1 6 
You can also try this code with Online PHP Compiler
Run Code

Also see,  Rabin Karp Algorithm

Conclusion

This article extensively discussed how to interchange the elements of the first and last row in a matrix.

We hope you have learned something new from this blog. And if you're interested in learning more, see our posts on Operations On MatricesConstruct a Linked List from a 2D MatrixMatrix Chain MultiplicationTypes of MatrixSet Matrix Zeros. Please vote for our blog to help other ninjas succeed.

Visit our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Happy Reading!

 

Live masterclass