0

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;
}
  • 1
    one idea is to do read the matrix spiral-wise and keep track of the last seen element and replace it with the current one and set the current as the last seen one. – user1984 Mar 31 '22 at 11:19
  • 2
    Just write your algorithm on a piece of paper first, trying to think of all possible conditions, instead of jumping into code immediately. Example: each element in col==0 and row==x must be moved to col=0 and row=x-1 (except element in row==0); then, each element in row==0 and col==y must be moved to row=0 and col=y+1 (except element in the last column), etc ... and since this is C++ exercise, use `std::vector` instead of raw pointers. – pptaszni Mar 31 '22 at 11:21
  • can you show how a 4x4 or 5x5 matrix is supposed to be rorated? Also, is input always a square matrix or could be mxn, in which case, what is the expected output after rotation. If you write enough examples, I'm sure there will arise a pattern which can be put into some kind of formula – vish4071 Mar 31 '22 at 11:27
  • 1
    Put your matrix on a coordinate plane and express the rotation in algebra. Then translate that to code. – Passer By Mar 31 '22 at 11:31
  • +1: show example with a 4x4 or 5x5 matrix. Is a "unitary" rotation of one only position the only thing requested to do? – Wisblade Mar 31 '22 at 11:47
  • btw you can rotate matrix by any angle see [How can I rotate a 2d array by LESS than 90°, to the best approximation?](https://stackoverflow.com/a/40585975/2521214) – Spektre Apr 01 '22 at 07:58

1 Answers1

1

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 
Arty
  • 14,883
  • 6
  • 36
  • 69