Hieu Havertz
Hieu Havertz

Reputation: 9

How can I rotate a matrix a clockwise?

Here's an example:

1 2 3
4 5 6
7 8 9

After rotating:

4 1 2
7 5 3
8 9 6

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

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

I can rotate 90° but that's not true in this exercise A element of the matrix just move once I have tried for 2 hours to find the algorithm but my code doesn't work. Please help me solve this problem

#include <iostream>
using namespace std;

void input(int **a,int row,int column)
{
    for(int i=0; i<row; i++)
    {
        for(int j=0; j<column; j++)
            cin>>a[i][j];
    }
}

void output(int **a,int row,int column)
{
    for(int i=0; i<row; i++)
    {
        for(int j=0; j<column; j++)
            cout<<a[i][j]<< " ";
        cout<<endl;
    }
}

void rotate(int **a,int **b,int m,int n)
{
    for(int i=0; i<n; i++)
    {
        int k=m-1;
        for(int j=0; j<m; j++)
        {
            b[i][j]=a[k][i];
            k--;
        }
    }
}

int main()
{
    int m,n;
    cin>>m>>n;

    int **a=new int*[m];
    for(int i=0; i<m; i++)
        a[i]=new int[n];

    int **b=new int*[n];
    for(int i=0; i<n; i++)
        b[i]=new int[m];

    input(a,m,n);
    rotate(a,b,m,n);
    output(b,n,m);
    return 0;
}

Upvotes: 0

Views: 581

Answers (1)

Arty
Arty

Reputation: 16767

Although I didn't find some single mathematical formula for this rotation, I did it using four tiny loops.

Also my program supports any arbitrary size of matrix NxM, not only square, and not only odd sizes.

For simplicity of running following code snippet instead of reading matrix from std::cin, I inlined values of matrix elements as constant in code.

Also for simplicity I used std::vector not to do any new/delete operations of plain matrix. It is quite obvious how to adopt my solution to your case of plain array.

Try it online!

#include <vector>
#include <iomanip>
#include <iostream>

void output(auto const & a) {
    for (size_t i = 0; i < a.size(); ++i) {
        for (size_t j = 0; j < a[i].size(); ++j)
            std::cout << std::setw(2) << a[i][j] << " ";
        std::cout << std::endl;
    }
}

void rotate(auto & a) {
    int i_first = 0, i_last = a.size() - 1,
        j_first = 0, j_last = a[0].size() - 1;
    auto b = a;
    while (i_first <= i_last && j_first <= j_last) {
        for (int j = j_first + 1; j <= j_last; ++j)
            b[i_first][j] = a[i_first][j - 1];
        for (int i = i_first + 1; i <= i_last; ++i)
            b[i][j_last] = a[i - 1][j_last];
        for (int j = j_last - 1; j >= j_first; --j)
            b[i_last][j] = a[i_last][j + 1];
        for (int i = i_last - 1; i >= i_first; --i)
            b[i][j_first] = a[i + 1][j_first];
        ++i_first; --i_last;
        ++j_first; --j_last;
    }
    a = b;
}

int main() {
    std::vector<std::vector<int>> a = {
        { 0,  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, 26, 27, 28, 29},
    };
    std::cout << "Before:" << std::endl;
    output(a);
    rotate(a);
    std::cout << "After:" << std::endl;
    output(a);
}

Output:

Before:
 0  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 26 27 28 29 

After:
 6  0  1  2  3  4 
12 13  7  8  9  5 
18 19 15 14 10 11 
24 20 21 22 16 17 
25 26 27 28 29 23 

Upvotes: 1

Related Questions