Rotating square matrix many times!

Hello everyone! Here I have a code that rotates the square matrix clokcwise and I am trying to do that I can rotate it k times (k is inputed by the user). I tried to use three loops, first: looping till i value reaches k and inside that the second and the third: looping through the matrix itself, but that did not work, so here below is only the rotating algorithm and maybe anyone can give me a hint on how to implement what I want?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include <iostream>

using namespace std;

int main()
{
	int matrix[50][50], n, k;
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cin >> matrix[i][j];
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = n - 1; j >= 0; j--)
		{
			cout << matrix[j][i] << " ";
		}
		cout << endl;
	}
}
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
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;

#define matrix vector< vector<T> >
int WIDTH = 3;


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


template <typename T> void print( const matrix &M )
{
   for ( auto row : M )
   {
      for ( auto e : row ) cout << setw( WIDTH ) << e << ' ';
      cout << '\n';
   }
   cout << '\n';
}


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


template <typename T> matrix quarterTurns( const matrix &M, int q )      
{                                                  // Rotate a SQUARE matrix by q quarter turns clockwise
   q = q % 4;                                      // Effective number of quarter turns (0-3)
   int N = M.size();
   matrix R = M;
   if ( q == 0 ) return R;                         // Identity

   for ( int i = 0; i < N; i++ )
   {
      for ( int j = 0; j < N; j++ )
      {
         int ii, jj;                               // Indices that i and j map to in the rotated matrix
         switch( q )
         {
            case 1: ii = j        ;   jj = N - 1 - i;   break;
            case 2: ii = N - 1 - i;   jj = N - 1 - j;   break;
            case 3: ii = N - 1 - j;   jj = i        ;   break;
         }
         R[ii][jj] = M[i][j];
      }
   }

   return R;
}


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


int main()
{
// vector< vector<int> > M = { { 1, 2, 3 },
//                             { 4, 5, 6 },
//                             { 7, 8, 9 }  };
//
   vector< vector<int> > M = { {  1,  2,  3,  4 },
                               {  5,  6,  7,  8 },
                               {  9, 10, 11, 12 },
                               { 13, 14, 15, 16 } };

   for ( int q = 0; q < 4; q++ ) print( quarterTurns( M, q ) );
}

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

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

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

  4   8  12  16 
  3   7  11  15 
  2   6  10  14 
  1   5   9  13 
Last edited on
Topic archived. No new replies allowed.