For example, the square matrix below:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
The result is as follows:
13 9 5 1
14 10 6 2
15 11 7 3
16 12 8 4
[Cpp]
# Include <stdio. h>
Void rotate (int * x, int rank)
{
Int * y = (int *) malloc (sizeof (int) * rank );
For (int I = 0; I <rank * rank; I ++)
{
// The number of rows analyzed in the original first row is changed to the same column. The original first column is used as an example to change to the same row. (I % rank) * rank changes the column to row rank-(I/rank)-1) changes the row to Column
Y [(I % rank) * rank + (rank-(I/rank)-1)] = x [I];
}
For (I = 0; I <rank * rank; I ++)
{
X [I] = y [I];
}
Free (y );
}
Int main (int argc, char * argv [])
{
Int x [4] [4] = {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16 }};
Int rank = 4;
Rotate (& x [0] [0], rank );
For (int I = 0; I <rank; I ++)
{
For (int j = 0; j <rank; j ++)
{
Printf ("% 4d", x [I] [j]);
}
Printf ("\ n ");
}
Return 0;
}
# Include <stdio. h>
Void rotate (int * x, int rank)
{
Int * y = (int *) malloc (sizeof (int) * rank );
For (int I = 0; I <rank * rank; I ++)
{
// The number of rows analyzed in the original first row is changed to the same column. The original first column is used as an example to change to the same row. (I % rank) * rank changes the column to row rank-(I/rank)-1) changes the row to Column
Y [(I % rank) * rank + (rank-(I/rank)-1)] = x [I];
}
For (I = 0; I <rank * rank; I ++)
{
X [I] = y [I];
}
Free (y );
}
Int main (int argc, char * argv [])
{
Int x [4] [4] = {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16 }};
Int rank = 4;
Rotate (& x [0] [0], rank );
For (int I = 0; I <rank; I ++)
{
For (int j = 0; j <rank; j ++)
{
Printf ("% 4d", x [I] [j]);
}
Printf ("\ n ");
}
Return 0;
}