jeudi 5 avril 2018

How to rotate outer ring of a matrix by n step in C++?

I want to rotate just the outer ring of a matrix clockwise.

Where n=number of steps to be rotate clockwise.

Suppose if I have a 4x5 matrix.

1 2 3 4 5

6 7 8 9 0

5 4 2 5 7

8 2 7 9 3

Now if n=1 the output should be :-

6 1 2 3 4

5 7 8 9 5

8 4 2 5 0

2 7 9 3 7

I have tried the logic of :

int temp = im[i][j];
          im[i][j] = im[n-1-j][i];
          im[n-1-j][i] = im[n-1-i][n-1-j];
          im[n-1-i][n-1-j] = im[j][n-1-i];
          im[j][n-1-i] = temp;

But I know this logic is completely wrong as it is moving the whole matrix.

Aucun commentaire:

Enregistrer un commentaire