Thursday, July 12, 2012

Write a program to rotate a matrix by 90 degrees

e.g)
Input:-
1 2 3
4 5 6
7 8 9

Output:-
7 4 1
8 5 2
9 6 3

Approach to rotate a matrix by 90 degrees

First we transpose the matrix and swap the columns to rotate the matrix by 90 degrees.
1) Transpose the matrix.
1 4 7
2 5 8
3 6 9

2) Swap the columns.
7 4 1
8 5 2
9 6 3

C++ program to rotate a matrix by 90 degress

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main() {
    srand(time(0));

    int m = 3;
    int n = 3;
    int** matrix = (int**) calloc(sizeof(int), m);
    for ( int i = 0; i < m; i++ ) {
        matrix[i] = (int*) calloc(sizeof(int), n);
    }

    // Fill random values in the matrix
    for ( int i = 0; i < m; i++ ) {
        for ( int j = 0; j < n; j++ ) {
            matrix[i][j] = rand() % 100;
        }
    }

    // Print the matrix
    for ( int i = 0; i < m; i++ ) {
        for ( int j = 0; j < n; j++ ) {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }

    cout << endl;

    // Transpose the matrix
    for ( int i = 0; i < m; i++ ) {
        for ( int j = i + 1; j < n; j++ ) {
            int tmp = matrix[i][j];
            matrix[i][j] = matrix[j][i];
            matrix[j][i] = tmp;
        }
    }

    // Swap the columns
    for ( int i = 0; i < m; i++ ) {
        for ( int j = 0; j < n/2; j++ ) {
            int tmp = matrix[i][j];
            matrix[i][j] = matrix[i][n-1-j];
            matrix[i][n-1-j] = tmp;
        }
    }

    // Print the rotated matrix
    for ( int i = 0; i < m; i++ ) {
        for ( int j = 0; j < n; j++ ) {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }
}

Output:-
28 66 73
67 21 63
87 54 59

87 67 28
54 21 66
59 63 73

6 comments :

  1. Liked your solution - simple and clean. I have also written in-place 90 degree rotation here

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. {code}

    #include
    #include

    void RotateMatrix(int (&mat)[4][4] );
    void RotateMatrix( int (&mat) [4][4] )
    {
    int mat2[4][4] = {0};
    for (int i = 0; i < 4; ++i)
    for (int j = 0; j < 1; ++j)
    {
    mat2[j][i] = mat[i][j];
    mat2[j + 1][i] = mat[i][j + 1];
    mat2[j + 2][i] = mat[i][j + 2];
    mat2[j + 3][i] = mat[i][j + 3];
    }
    for (int i = 0; i < 4; ++i){
    int temp = mat2[i][0];
    mat2[i][0] = mat2[i][3];
    mat2[i][3] = temp;
    temp = mat2[i][1];
    mat2[i][1] = mat2[i][2];
    mat2[i][2] = temp;
    mat[i][0] = mat2[i][0];
    mat[i][1] = mat2[i][1];
    mat[i][2] = mat2[i][2];
    mat[i][3] = mat2[i][3];
    }
    }
    int main(int argc, char* argv[]){
    const int r = 4 , c = 4;
    int mat[r][c] =
    {
    1 ,2 ,3 ,4,
    5 ,6 ,7 ,8,
    9 ,10 ,11 ,12,
    13 ,14 ,15 ,16
    };

    //Printing the array before rotation
    for (int i = 0; i < 4; ++i){
    for (int j = 0; j < 4; ++j){
    std::cout << mat[i][j] << " ";
    }
    std::cout << std::endl;
    }

    std::cout << "-------------" << std::endl;

    RotateMatrix(mat);

    //Printing the array after rotation
    for (int i = 0; i < 4; ++i){
    for (int j = 0; j < 4; ++j){
    std::cout << mat[i][j] << " ";
    }
    std::cout << std::endl;
    }

    system("pause");
    return 0;
    }

    {code}

    ReplyDelete
  5. {code}

    #include
    #include

    void RotateMatrix(int (&mat)[4][4] );
    void RotateMatrix( int (&mat) [4][4] )
    {
    int mat2[4][4] = {0};
    for (int i = 0; i < 4; ++i)
    for (int j = 0; j < 1; ++j)
    {
    mat2[j][i] = mat[i][j];
    mat2[j + 1][i] = mat[i][j + 1];
    mat2[j + 2][i] = mat[i][j + 2];
    mat2[j + 3][i] = mat[i][j + 3];
    }
    for (int i = 0; i < 4; ++i){
    int temp = mat2[i][0];
    mat2[i][0] = mat2[i][3];
    mat2[i][3] = temp;
    temp = mat2[i][1];
    mat2[i][1] = mat2[i][2];
    mat2[i][2] = temp;
    mat[i][0] = mat2[i][0];
    mat[i][1] = mat2[i][1];
    mat[i][2] = mat2[i][2];
    mat[i][3] = mat2[i][3];
    }
    }
    int main(int argc, char* argv[]){
    const int r = 4 , c = 4;
    int mat[r][c] =
    {
    1 ,2 ,3 ,4,
    5 ,6 ,7 ,8,
    9 ,10 ,11 ,12,
    13 ,14 ,15 ,16
    };

    //Printing the array before rotation
    for (int i = 0; i < 4; ++i){
    for (int j = 0; j < 4; ++j){
    std::cout << mat[i][j] << " ";
    }
    std::cout << std::endl;
    }

    std::cout << "-------------" << std::endl;

    RotateMatrix(mat);

    //Printing the array after rotation
    for (int i = 0; i < 4; ++i){
    for (int j = 0; j < 4; ++j){
    std::cout << mat[i][j] << " ";
    }
    std::cout << std::endl;
    }

    system("pause");
    return 0;
    }

    {code}

    ReplyDelete
  6. Good code.. Here is another simple program to rotate array by 90 degrees




    Rotate array by 90 degrees

    ReplyDelete

Contact Form

Name

Email *

Message *

Back to Top