In-place rotation of a matrix by 90 degrees in C++

In this tutorial, we will learn In-place rotation of a matrix by 90 degrees in c++. There are many ways to do it. We will be learning in-place rotation in this article. In-place rotation means changes are made in the original array.

A square matrix is a matrix in which the number of rows and columns are equal. Rotation by 90 degrees means:

Input:

1 2 3 

4 5 6

7 8 9

Output:

7 4 1 

8 5 2

9 6 3

Algorithm of In-place rotation of a matrix by 90 degrees in C++

We will first find the transpose of a matrix and then the inverse.

Transpose of a matrix:

The transpose of a matrix is a new matrix whose columns are the rows of the original. Basically, we need to swap the row and column elements. For example- a[0][0] remains a[0][0].All the diagonal elements remain the same in transpose. a[0][1] becomes a[1][0].

swap(a[i][j],a[j][i]);//this is the logic behind finding transpose of a matrix
Input:
1 2 3  
4 5 6 
7 8 9
transpose of this:
1	4	7	
2	5	8	
3	6	9

Input:

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

transpose of this:
1	5	9	13	
2	6	10	14	
3	7	11	15	
4	8	12	16


The Inverse of a matrix:

The inverse of a 3*3 matrix is as  follows:

Input:

1 2 3
4 5 6
7 8 9

Inverse:
3	2	1	
6	5	4	
9	8	7

and for a 4*4 matrix:

Input:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

Inverse:
4	3	2	1	
8	7	6	5	
12	11	10	9	
16	15	14	13

As we can observe a pattern here, the inverse of a matrix is obtained by reversing the elements of all rows. This is the algorithm we will be using to find the inverse of a matrix. And by first finding transpose and then the inverse of a matrix, the matrix will be rotated by 90 degrees.

This is the code for swapping 2 elements.

void swap(int & first, int & second)
  {
       int temp = first;
        first = second;
        second = temp; 
  } 

Here, is the code implementation:

C++ Program of In-place rotation of a matrix by 90 degrees

//by transpose and inverse 
#include<iostream>
 using namespace std; 
 
 void swap(int & first, int & second)
  {
       int temp = first;
        first = second;
        second = temp; 
  } 
  void transpose(int a[][10], int n)
   {
        int row = 0;
   
    while (row < n) 
   { for (int column = row; column < n; column++) 
   { swap(a[row][column], a[column][row]);
    } 
    row++;
     }
      }

 void inverse(int a[][10], int n) 
 { 
     for (int var = 0; var < (n / 2); var++)
  { for (int rowsofmatrix = 0; rowsofmatrix < n; rowsofmatrix++) 
  { swap(a[rowsofmatrix][var], a[rowsofmatrix][n - var - 1]);
   } 
   }
    }
    
 int main() 
 { int rowsofmatrix, columnsofmatrix, m, n, a[10][10];
  cin >> n;
   for (rowsofmatrix = 0; rowsofmatrix < n; rowsofmatrix++) 
   { for (columnsofmatrix = 0; columnsofmatrix < n; columnsofmatrix++)
    { cin >> a[rowsofmatrix][columnsofmatrix]; 
    }
     } 
     transpose(a, n); 
     inverse(a, n); 

for (rowsofmatrix = 0; rowsofmatrix < n; rowsofmatrix++)
 { for (columnsofmatrix = 0; columnsofmatrix < n; columnsofmatrix++) 
 { cout << a[rowsofmatrix][columnsofmatrix] << '\t';
  } cout << endl;
   }
}

 

Input:

4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

Output:

13 9 5 1 
14 10 6 2
15 11 7 3 
16 12 8 4

Input:
5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

Output:

21	16	11	6	1	
22	17	12	7	2	
23	18	13	8	3	
24	19	14	9	4	
25	20	15	10	5

Also, refer to:

Addition of 2 matrices in C++

Leave a Reply

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