### Rotating matrix problem

Hi! I got the task where I must make a function in which I must rotate a dynamic, bool matrix and save this rotated form to another dynamic matrix in a void function:

`void rotateRight(bool** original, bool** rotated, int size)`

The making and initialization of the matrix is done outside the function, in main, I must only rotate the matrix and save it to the other one.

I thought of using the following method: https://www.google.hu/amp/s/www.geeksforgeeks.org/rotate-a-matrix-by-90-degree-in-clockwise-direction-without-using-any-extra-space/amp/

However, I have trouble with the rotated matrix. It's almost good to go - except the last column, where, according to my example, there shouldn't be any 'X', which mark the 'true' elements in the array (so the dots mark the 'false' elements). Could you help me out, please?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101`` ``````#include bool** allocMtx(int size); void print(bool** mtx, int size); void rotateRight(bool** original, bool** rotated, int size); void freeMtx(bool** mtx, int size); int main(){ const int size = 5; bool** one = allocMtx(size); bool** two = allocMtx(size); one[1][1] = one[1][3] = one[3][0] = one[4][1] = one[4][2] = one[4][3] = one[3][4] = true; std::cout << "The original matrix:\n"; print(one, size); std::cout << "\n\n"; rotateRight(one, two, size); freeMtx(one, size); freeMtx(two, size); return 0; } bool** allocMtx(int size){ bool** mtx = new bool*[size]; if(mtx){ for(int i = 0; i < size; i++){ mtx[i] = new bool[size]; if(!mtx[i]){ exit(1); } } }else{ exit(1); } return mtx; } void rotateRight(bool** original, bool** rotated, int size){ rotated = original; for(int i = 0; i < size/2; i++){ for(int j = i; j < size-i-1; j++){ int temp = rotated[i][j]; rotated[i][j] = rotated[size-1-j][i]; rotated[size-1-j][i] = rotated[size-1-i][size-1-j]; rotated[size-1-i][size-1-j] = rotated[j][size-1-i]; rotated[j][size-1-i] = temp; } } std::cout << "\nThe rotated matrix:\n"; print(rotated, size); } void print(bool** mtx, int size){ int counter = 0; std::cout << ' '; for(int i = 0; i < size; i++){ std::cout << i; } std::cout << '\n'; for(int i = 0; i < size; i++){ std::cout << i; for(int j = 0; j < size; j++){ if(mtx[i][j] == false){ std::cout << '.'; }else{ std::cout << 'X'; } counter++; if(counter == size){ std::cout << '\n'; counter = 0; } } } } void freeMtx(bool** mtx, int size){ for(int i = 0; i < size; i++){ delete[] mtx[i]; }delete[] mtx; }``````

Here's what I got as the result:
The original matrix:
01234
0.....
1.X.X.
2.....
3X...X
4.XXX.

The rotated matrix:
01234
0.X..X
1X..XX
2X..XX
3X..X.
4.X...
 ``123`` ``````void rotateRight(bool** original, bool** rotated, int size){ rotated = original; // <===== NO! NO! NO! ``````

The highlighted line does not do what you think it does.

To make the code give you an appropriate answer (but still be lousy code):
- erase line 48
- make the call on line 21 `rotateRight(one, one, size);`

To fix it permanently, either:
(1) use std::vector for arrays, so that you have defined copy operations;
or
(2) copy your arrays correctly (not just the initial pointer-to-pointer)
or
(3) just use a single array in the first place!

Actually, if you are going to make a copy anyway ... why try to save space?

Example of (1):
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253`` ``````#include #include using namespace std; using matbool = vector< vector >; void print( const matbool &mtx ); matbool rotateRight( matbool mtx ); int main() { const int size = 5; matbool one( size, vector( size, false ) ); one[1][1] = one[1][3] = one[3][0] = one[4][1] = one[4][2] = one[4][3] = one[3][4] = true; matbool two = rotateRight( one ); cout << "Original matrix:\n"; print( one ); cout << "\n\n"; cout << "Rotated matrix:\n"; print( two ); } matbool rotateRight( matbool mtx ) // Pass by value ... makes a COPY automatically { int size = mtx.size(); for ( int i = 0; i < size / 2; i++ ) { for ( int j = i; j < size - i - 1; j++ ) { int temp = mtx[i][j]; mtx[i][j] = mtx[size-1-j][i]; mtx[size-1-j][i] = mtx[size-1-i][size-1-j]; mtx[size-1-i][size-1-j] = mtx[j][size-1-i]; mtx[j][size-1-i] = temp; } } return mtx; } void print( const matbool &mtx ) { int size = mtx.size(); cout << ' '; for ( int i = 0; i < size; i++ ) cout << i; cout << '\n'; for ( int i = 0; i < size; i++ ) { cout << i; for ( int j = 0; j < size; j++ ) cout << ".X"[mtx[i][j]]; cout << '\n'; } }``````

 ```Original matrix: 01234 0..... 1.X.X. 2..... 3X...X 4.XXX. Rotated matrix: 01234 0.X... 1X..X. 2X.... 3X..X. 4.X...```
Last edited on
Or just
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344`` ``````#include #include using namespace std; using matbool = vector< vector >; //============================ matbool rotateRight( const matbool &original ) { int size = original.size(); matbool mtx( size, vector( size ) ); for ( int i = 0; i < size; i++ ) for ( int j = 0; j < size; j++ ) mtx[i][j] = original[size-1-j][i]; return mtx; } //============================ void print( const matbool &mtx ) { int size = mtx.size(); cout << ' '; for ( int i = 0; i < size; i++ ) cout << i; cout << '\n'; for ( int i = 0; i < size; i++ ) { cout << i; for ( int j = 0; j < size; j++ ) cout << ".X"[mtx[i][j]]; cout << '\n'; } } //============================ int main() { const int size = 5; matbool one( size, vector( size, false ) ); one[1][1] = one[1][3] = one[3][0] = one[4][1] = one[4][2] = one[4][3] = one[3][4] = true; cout << "Original matrix:\n"; print( one ); cout << "\nRotated matrix:\n"; print( rotateRight( one ) ); }``````

 ```Original matrix: 01234 0..... 1.X.X. 2..... 3X...X 4.XXX. Rotated matrix: 01234 0.X... 1X..X. 2X.... 3X..X. 4.X...```
Ooo thank you very much, I get it now :D
Topic archived. No new replies allowed.