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;
	}
}
You could very carefully rotate the matrix without losing any data, using a different method than simple loops.

But since RAM is like 5 or 10 bugs a gigabyte these days, maybe you should just define two arrays and copy one into the other when you rotate it. Just saying..
Topic archived. No new replies allowed.