How to swap both diagonals of a matrix in C++

In this tutorial, we will learn how to swap boh the diagonals of a matrix in C++ with an example, algorithm.

The square matrix has two diagonals. one, which starts from the top-left corner and ends at the bottom-right corner ( let it be named as diagonal – 1). second, which starts from the top-right corner and ends ar the bottom-left corner (let it be named as diagonal-2).

Therefore, we have to swap diagonal-1 to diagonal-2 of a given square matrix.

How to swap both diagonals of a matrix

let’s take some example to understand this in a better way:

Example 1:

Input matrix:
              8 7 5
              4 1 2
              7 9 3
here, the elements of first diagonal (diagonal-1) is : 8 1 3
and elements of second diagonal (diagonal-2) is: 5 1 7

after swaping, diagonal-1 and diagonal-2, 
we will get matrix:  5 7 8
                     4 1 2
                     3 9 7


 Example 2:

Input matrix: 
              1 2 3 4
              5 6 7 8
              9 0 1 2
              3 4 5 6

here, the elements of first diagonal (diagonal-1) is : 1 6 1 6
and elements of second diagonal (diagonal-2) is: 4 7 0 3

after swaping, diagonal-1 and diagonal-2, 
we will get matrix:   4 2 3 1
                      5 7 6 8
                      9 1 0 2
                      6 4 5 3

The approach to this problem is simple. we just need to swap each diagonal element of the same row and do the same for rest of the rows of the matrix. just remember, the diagonal-1 element and sum of indexes of diagonal-2 is equal to the size of Matrix.

Following is the implementation for the above approach.

C++ program to swap both diagonal of a matrix

#include <iostream>
using namespace std; 

#define size 50 
  
//Function to swap diagonal of matrix 
void swapDiagonal(int matrix[size][size], int n, int m) { 

    int temp;

  for (int i = 0; i < n; i++){
      temp = matrix[i][i];
      matrix[i][i] = matrix[i][n - i - 1];
      matrix[i][n - i - 1] = temp;
  }
    
} 
   
int main() { 
  
  int matrix[size][size], n, m;

  cout<<"Enter the length of row of matrix: ";
  cin>>m;
  cout<<"Enter the length of column of matrix: ";
  cin>>n;

  if(n != m){
       cout<<"length of row and column should be same.";
       exit(0);
   }

    //taking input in the matrix
    cout<<"input the element of matrix:\n";
    for(int i = 0; i < m; i++)
        for(int j =0; j < n; j++)
            cin>>matrix[i][j]; 

   //displaying matrix before perform swap
   cout<<"\nMatrix before swapping diagonals:\n";
   for (int i = 0; i < m; i++) { 
        for (int j = 0; j < n; j++) 
            cout << matrix[i][j] << " "; 

        cout << endl; 
   } 

  swapDiagonal(matrix, n, m); 
  
  //displaying modified matrix 
  cout<<"\nMatrix After swapping diagonals:\n";
  for (int i = 0; i < m; i++) { 
    for (int j = 0; j < n; j++) 
      cout << matrix[i][j] << " "; 

    cout << endl; 
  } 
  
  return 0; 
}

Output: 

Enter the length of row of matrix: 4
Enter the length of column of matrix: 4
input the element of array:
2 5 7 2
3 6 8 2
5 8 0 9
1 3 5 6

Matrix before swapping diagonals:
2 5 7 2
3 6 8 2
5 8 0 9
1 3 5 6

Matrix After swapping diagonals:
2 5 7 2
3 8 6 2
5 0 8 9
6 3 5 1

Time Complexity: O(m*n) where m and n are the lengths of row and column respectively.

You may also read: 

  1. Find sum of each row and column of a matrix in C++
  2. Print the corner elements of the matrix using C++

Leave a Reply

Your email address will not be published. Required fields are marked *