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 Matrices, Construct a Linked List from a 2D Matrix, Matrix Chain Multiplication, Types of Matrix, Set 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!