# Matrix rotation by circulating

Discussion in 'C' started by tomas7470, May 5, 2012.

1. ### tomas7470New Member

Joined:
May 5, 2012
Messages:
3
0
Trophy Points:
0
i need to write a code that solve this issue...
if the number of circulating was 4 then....
if the matrix first was :
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
Then after 4 rotation to the right the matrix will be:
2 1 1 1
3 2 3 1
4 2 3 2
4 4 4 3
the input could be any number of circulating...

thank Tomas

2. ### skr1986New Member

Joined:
Aug 2, 2012
Messages:
4
0
Trophy Points:
0
Hope this is what u asked...
Code:
```/*
*author SHIBIN K.REENY(skr1986.wordpress.com)
*/

#include <stdio.h>
main()
{         int a, i, j, k, e1, e2, e3, e4, rotate;
for(i = 0; i < 4; i++)for(j = 0; j < 4; j++)
scanf("%d", &a[i][j]);
printf("Enter the rotation number : ");
scanf("%d", &rotate);
printf("Orginal matrix :-\n");
for(i = 0; i < 4; i++){           // displaying the original matrix
for(j = 0; j < 4; j++)
printf("%d  ", a[i][j]);
printf("\n");
}
printf("\n");
k = 0;
while(k < rotate){
// saving the edges...
e1 = a;
e2 = a;
e3 = a;
e4 = a;
for(i = 3; i > 0; i--)         // top edge moving by one position
a[i] = a[i - 1];
for(i = 3; i > 1; i--)         // rigth edge moving by one position
a[i] = a[i - 1];
a = e2;
for(i = 0; i < 2; i++)         // bottom edge moving by one position
a[i] = a[i + 1];
a = e3;
for(i = 0; i < 2; i++)         // left edge moving by one position
a[i] = a[i + 1];
a = e4;
k++;
}
printf("NEW Matrix :-\n");
for(i = 0; i < 4; i++){            // final output after rotation
for(j = 0; j < 4; j++)
printf("%d  ", a[i][j]);
printf("\n");
}
return 1;
}```