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?