Javatpoint Logo
Javatpoint Logo

C++ Program to Swap Rows of Matrix

In many branches of mathematics and computer science, manipulating matrix is an essential process. Row shifting in a matrix is one often performed operation. It can help in rearranging data and improving computations, among other things.

Introduction to Matrix:

A two-dimensional collection of numbers organized in rows and columns is called a matrix. It is frequently used to carry out mathematical operations and represent data. Columns run vertically, and rows run horizontally in a conventional matrix.

For example, consider 3*3 matrix:

Algorithm: Matrix Row Swapping


  • A 2D matrix of size M*N.
  • To swap, use row indices row1 and row 2.


  • The matrix mat where rows 1 and 2 are swapped.

Step 1:

  • Check if the input indices are valid.
  • Make sure rows 1 and 2 are in the allowed range, which is 0 to M-1 (M being the number of rows in the matrix).

Step 2:

  • Swap the rows.
  • Create a temporary row or array called temp to save the values of row row1.
  • Perform the following actions for each column col between 0 and N-1 (where N is the number of columns in the matrix):
  • Assign row row1 values to temp[col] = mat[row1][col].
  • Assign mat[row1][col] = mat[row2][col] to replicate the data from row row2 to row row1.
  • Assign mat[row2][col] = temp[col] to duplicate the values from temp to row row2.

Step 3:

  • Give the updated matrix mat back.

Pseudo Code:


Let's take an C++ program to swap rows of matrix:


C++ Program to Swap Rows of Matrix

Code Explanation:

Step 1:

  • Add Any Required Header.
  • The required header for the C++ standard library is first included in the code. Operations involving input and output depend on this header.

Step 2:

  • After that, define the swapRows Function.
  • A matrix's two rows can be switched using the swapRows method.
  • It requires two-row indices (row1 and row2), which indicate the rows to be switched, and a 2D matrix (matrix) of size 3x3.

Step 3:

  • Initialize the Main Function.
  • A 3x3 matrix is declared and initialized with values in the main function. The data that will be worked on is represented by this matrix.

Step 4:

  • Input Row Numbers to Swap.
  • When the user wants to swap rows 1 and 2, the program asks them to enter those numbers. These variables contain the input from the user.

Step 5:

  • Validate Input Row Numbers.
  • The application verifies the validity of the input row numbers. The range of valid rows in a 3x3 matrix is between 0 and 2, inclusive, for both row1 and row2 to be considered valid.

Step 6:

  • Call the swapRows Function.
  • The swapRows method is called if the row numbers are valid. This function swaps the rows by iterating through the columns and temporarily storing and exchanging the elements of the two specified rows.


  • Display the Modified Matrix.
  • The program shows the updated matrix after flipping the rows. Iterating through the matrix's rows and columns using stacked loops, it prints each element. Now, the user is presented with the outcome of the row swapping process.

Step 8:

  • Handle Invalid Row Numbers.
  • The application notifies the user that the row numbers they entered are invalid and requests that they provide rows between 0 and 2.

Step 9:

  • Return 0 and End the Program.
  • The program ends when the main function returns 0, which denotes that the program has been executed successfully.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA