Interchange elements of first and last rows in matrix
Given a 4 x 4 matrix, we have to interchange the elements of first and last row and show the resulting matrix.
Examples :
Input : 3 4 5 0
2 6 1 2
2 7 1 2
2 1 1 2
Output : 2 1 1 2
2 7 1 2
2 6 1 2
3 4 5 0
Input : 9 7 5 1
2 3 4 1
5 6 6 5
1 2 3 1
Output : 1 2 3 1
2 3 4 1
5 6 6 5
9 7 5 1
The approach is very simple, we can simply swap the elements of first and last row of the matrix inorder to get the desired matrix as output.
Below is the implementation of the approach :
C++
// C++ code to swap the element of first // and last row and display the result #include <iostream> using namespace std; #define n 4 void interchangeFirstLast(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 m[n][n] = { { 8, 9, 7, 6 }, { 4, 7, 6, 5 }, { 3, 2, 1, 8 }, { 9, 9, 7, 7 } }; interchangeFirstLast(m); // printing the interchanged matrix for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cout << m[i][j] << " "; cout << endl; } } // This code is contributed by Anant Agarwal. |
chevron_right
filter_none
Java
// Java code to swap the element of first // and last row and display the result import java.io.*; public class Interchange { static void interchangeFirstLast(int m[][]) { int rows = m.length; // swapping of element between first // and last rows for (int i = 0; i < m[0].length; i++) { int t = m[0][i]; m[0][i] = m[rows-1][i]; m[rows-1][i] = t; } } // Driver code public static void main(String args[]) throws IOException { // input in the array int m[][] = { { 8, 9, 7, 6 }, { 4, 7, 6, 5 }, { 3, 2, 1, 8 }, { 9, 9, 7, 7 } }; interchangeFirstLast(m); // printing the interchanged matrix for (int i = 0; i < m.length; i++) { for (int j = 0; j < m[0].length; j++) System.out.print(m[i][j] + " "); System.out.println(); } } } |
chevron_right
filter_none
Python3
# Python code to swap the element # of first and last row and display # the result def interchangeFirstLast(mat, n, m): rows = n # swapping of element between # first and last rows for i in range(n): t = mat[0][i] mat[0][i] = mat[rows-1][i] mat[rows-1][i] = t # Driver Program mat = [[8, 9, 7, 6], [4, 7, 6, 5], [3, 2, 1, 8], [9, 9, 7, 7]] n = 4m = 4interchangeFirstLast(mat, n, m) # printing the interchanged matrix for i in range(n): for j in range(m): print(mat[i][j], end = " ") print("\n") # This code is contributed by Shrikant13. |
chevron_right
filter_none
C#
// C# code to swap the element of first // and last row and display the result using System; class GFG { public static void interchangeFirstLast(int[][] m) { int rows = m.Length; // swapping of element between first // and last rows for (int i = 0; i < m[0].Length; i++) { int t = m[0][i]; m[0][i] = m[rows - 1][i]; m[rows - 1][i] = t; } } // Driver code public static void Main(string[] args) { // input in the array int[][] m = new int[][] { new int[] {8, 9, 7, 6}, new int[] {4, 7, 6, 5}, new int[] {3, 2, 1, 8}, new int[] {9, 9, 7, 7} }; interchangeFirstLast(m); // printing the interchanged matrix for (int i = 0; i < m.Length; i++) { for (int j = 0; j < m[0].Length; j++) { Console.Write(m[i][j] + " "); } Console.WriteLine(); } } } // This code is contributed by Shrikant13 |
chevron_right
filter_none
PHP
<?php // PHP code to swap the element of first // and last row and display the result $n = 4; function interchangeFirstLast(&$m) { global $n; $rows = $n; // swapping of element between first // and last rows for ($i = 0; $i < $n; $i++) { $t = $m[0][$i]; $m[0][$i] = $m[$rows - 1][$i]; $m[$rows - 1][$i] = $t; } } // Driver function // input in the array $m = array(array(8, 9, 7, 6), array(4, 7, 6, 5), array( 3, 2, 1, 8), array(9, 9, 7, 7)); interchangeFirstLast($m); // printing the interchanged matrix for ($i = 0; $i < $n; $i++) { for ($j = 0; $j < $n; $j++) echo $m[$i][$j] . " "; echo "\n"; } ?> |
chevron_right
filter_none
Output :
9 9 7 7 4 7 6 5 3 2 1 8 8 9 7 6
Recommended Posts:
- Interchange elements of first and last columns in matrix
- Common elements in all rows of a given matrix
- Maximum difference of sum of elements in two rows in a matrix
- Find distinct elements common to all rows of a matrix
- Program to Interchange Diagonals of Matrix
- Count all sorted rows in a matrix
- Remove first X rows and columns from a matrix
- Find all permuted rows of a given row in a matrix
- Remove any corner X rows and columns from a matrix
- Count rows in a matrix that consist of same element
- Print unique rows in a given boolean matrix
- Check if all rows of a matrix are circular rotations of each other
- Find duplicate rows in a binary matrix
- Find pair of rows in a binary matrix that has maximum bit difference
- Python Counter| Find duplicate rows in a binary matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



