Function to rotate the matrix by 90 degrees
| 19 | |
| 20 | //Function to rotate the matrix by 90 degrees |
| 21 | void rotate(vector<vector<int> >& matrix) |
| 22 | { |
| 23 | int r=matrix.size(); //Stores no. of rows in the matrix |
| 24 | int c=matrix[0].size(); //Stores no. of columns in the matrix |
| 25 | for(int i=0;i<r;i++){ |
| 26 | for(int j=i+1;j<c;j++){ |
| 27 | //Swapping the element at position (i,j) with position (j,i) |
| 28 | int temp=matrix[i][j]; |
| 29 | matrix[i][j]=matrix[j][i]; |
| 30 | matrix[j][i]=temp; |
| 31 | } |
| 32 | } |
| 33 | for(int i=0;i<r/2;i++)//Loop which travels through only one half of the number of rows |
| 34 | { |
| 35 | for(int j=0;j<c;j++){ |
| 36 | //Swapping the elements in the first half of the rows with the second half of the rows |
| 37 | swap(matrix[i][j],matrix[r-1-i][j]); |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | int main() |
| 43 | { |