0

How can I rotate by 90 degrees a 3 x 3 matrix in cpp...

//create the matrix
const int MATRIX_SIZE = 3;
int m[MATRIX_SIZE][MATRIX_SIZE];
int last = MATRIX_SIZE - 1;

int count = 0;
int level = 0;


for(int i =0; i< MATRIX_SIZE; i++)
{
    for(int j = 0; j< MATRIX_SIZE; j++)
    {
        m[i][j] = count++;
    }
}

//rotate matrix
   for(int i = 0; i <last; ++i)
    {
        Swap(m[level][i], m[i][last]);
        Swap(m[level][i], m[last][last-i]);
        Swap(m[level][i], m[last-i][level]);
    }

I get a lot of 1's added and I do not know where they are coming from.

OUTPUT:

------ Original Matrix ------ 
0       1       2       
3       4       5       
6       7       8       
 ------ Rotated Matrix ------ 
6       3       1       
1       4       1       
1       1       1     

I am using "level" to swal through the inner part of the matrix. Is there any other way to do this using STD?

MpcHAG
  • 19
  • 1
  • 7
  • 3
    `m[level][i]` is being swapped in all three swaps. This can't be what you want. Typo? – user4581301 Apr 05 '22 at 01:45
  • 1
    Good chance that `last` is out of bounds based on your loop condition. If you need help, please [edit] your question and provide a [mcve]. – Stephen Newell Apr 05 '22 at 01:46
  • Re: _"Is there any other way to do this?"_ -- you should first focus on fixing the way you've chosen to do it. – paddy Apr 05 '22 at 01:47
  • 1
    Here's how to figure this out, this always works! Take out a blank sheet of paper. Write down a step-by-step process of doing this, as short, brief sentences in plain English. [Have your rubber duck review your plan](https://en.wikipedia.org/wiki/Rubber_duck_debugging). Generally, we don't write or debug other people's code, on Stackoverflow. We refer such questions to their rubber duck. After your rubber duck approves your proposed plan of action, simply take what you've written down and translate it directly into C++, and you're done! Have you discussed this with your rubber duck, yet? – Sam Varshavchik Apr 05 '22 at 01:50
  • One thing you may wish to consider, alongside other suggestions, is using a template. Something like ` – Chris Apr 05 '22 at 02:20

2 Answers2

1

Presented for your consideration. Using templates to infer the size of the matrix as it's passed into functions to print it, and to rotate it 90 degrees counter-clockwise.

In the rotate_matrix function, the outer loop iterates over "shell levels" using sl. A matrix of 3x3 dimensions has 3 / 2 (or 1) shell levels that need to be rotated. I've tested with a 6x6 matrix, which has three shell levels.

To avoid repetition, each shell has a dimension of N - sl * 2, and we subtract one from that to avoid repetition of work on the corners.

Then we just need to map out the four values that need to be moved on each iteration, and do a four-way swap.

#include <iostream>

template <typename T, std::size_t N>
void print_matrix(T m[N][N]) {
    for (std::size_t i = 0; i < N; ++i) {
        for (std::size_t j = 0; j < N; ++j) {
            std::cout << m[i][j] << " ";
        }

        std::cout << std::endl;
    }
}

template <typename T, std::size_t N>
void rotate_matrix(T m[N][N]) {
    for (std::size_t sl = 0; sl < N / 2; ++sl) {
        for (std::size_t i = 0; i < N - 1 - sl * 2; ++i) {
            T temp                       = m[sl][sl + i];          
            m[sl][sl + i]                = m[sl + i][N - 1 - sl];
            m[sl + i][N - 1 - sl]        = m[N - 1 - sl][N - 1 -sl - i];
            m[N - 1 - sl][N - 1 -sl - i] = m[N - 1 - sl - i][sl];
            m[N - 1 - sl - i][sl]        = temp;
        }
    }
}

int main() {
    int m[6][6] = {
        { 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, 30},
        {31, 32, 33, 34, 35, 36}
    };

    print_matrix(m);

    std::cout << std::endl;

    rotate_matrix(m);
    print_matrix(m);

    return 0;
}

Result:

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 30
31 32 33 34 35 36

6 12 18 24 30 36
5 11 17 23 29 35
4 10 16 22 28 34
3 9 15 21 27 33
2 8 14 20 26 32
1 7 13 19 25 31
Chris
  • 26,361
  • 5
  • 21
  • 42
0

the swap() fucntion had an typo. got it to work

void Swap(int& i, int& j)
{
    int temp = i; 
    i = j; 
    j = temp;
}

Also, check this out for other solutions. How to rotate a N x N matrix by 90 degrees?

MpcHAG
  • 19
  • 1
  • 7