C++ Program to Interchange elements of first and last rows in matrix Last Updated : 07 Jun, 2022 Comments Improve Suggest changes Like Article Like Report 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 6 1 2 2 7 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 code 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. Output : 9 9 7 7 4 7 6 5 3 2 1 8 8 9 7 6 Time Complexity: O(N), as we are using a loop to traverse N times. Auxiliary Space: O(1), as we are not using any extra space. Please refer complete article on Interchange elements of first and last rows in matrix for more details! Comment More infoAdvertise with us Next Article C++ Program to Interchange elements of first and last rows in matrix K kartik Follow Improve Article Tags : C++ Practice Tags : CPP Similar Reads Print 2D matrix in different lines and without curly braces in C/C++? Following is a general way of printing 2D matrix such that every row is printed in separate lines. C for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { cout << arr[i][j] << " "; } // Newline for new row cout << endl; } How to print without using any curly b 2 min read Cpp14 Program to Turn an image by 90 degree Given an image, how will you turn it by 90 degrees? A vague question. Minimize the browser and try your solution before going further. An image can be treated as 2D matrix which can be stored in a buffer. We are provided with matrix dimensions and it's base address. How can we turn it? For example s 4 min read Convert given upper triangular Matrix to 1D Array Given an upper triangular matrix M[][] of dimensions N * N, the task is to convert it into an one-dimensional array storing only non-zero elements from the matrix. Examples: Input: M[][] = {{1, 2, 3, 4}, {0, 5, 6, 7}, {0, 0, 8, 9}, {0, 0, 0, 10}}Output: Row-wise: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} Colu 12 min read How to quickly swap two arrays of same size in C++? Given two arrays a[] and b[] of same size, we need to swap their contents. Example : Input: a[] = {1, 2, 3, 4} b[] = {5, 6, 7, 8}Output: a[] = {5, 6, 7, 8}b[] = {1, 2, 3, 4} A simple solution is to iterate over elements of both arrays and swap them one by one. A quick solution is to use std::swap(). 2 min read array::fill() and array::swap() in C++ STL Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::fill() This function is used to set a common value for all the elements of the array container. Syntax : a 3 min read Like