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?

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include<iostream>

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...
1
2
3
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):
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <vector>
using namespace std;

using matbool = vector< vector<bool> >;

void print( const matbool &mtx );
matbool rotateRight( matbool mtx );

int main()
{
   const int size = 5;
   matbool one( size, vector<bool>( 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
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
37
38
39
40
41
42
43
44
#include <iostream>
#include <vector>
using namespace std;

using matbool = vector< vector<bool> >;

//============================

matbool rotateRight( const matbool &original )
{
   int size = original.size();
   matbool mtx( size, vector<bool>( 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<bool>( 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.