# Thread: Matrix rotation by circulating

1. ## Matrix rotation by circulating

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. So break it down into steps
- shift top row right one place
- shift right column down one place
and so on.

When you've got a function to do the whole matrix for one step, then call it N times.

Take a look at the forum rules. We don't help people who just post "I want to do...." without showing any attempt at solving the problem (such as ideas or code).

3. i have been trying all day!!!

actually i made it but i need to do it in a different way... u see..
Code:
```
if(MatWid < MatHei)				{
Layers = MatWid / 2;
}
else
Layers = MatHei / 2;

/* According to the number of circulations required */
for (circ = 0 ; circ < numOfCir ; circ++)
{
/* According to the number of layers */
for (layer = 0 ; layer < Layers ; layer++)
{
int tmp1, tmp2;
/* Save upper right corner */
tmp1 = Matrix[layer][MatWid-layer-1];
/* Upper bound */
for (j = MatWid-layer-2 ; j >= layer ; j--)
{
Matrix[layer][j+1] = Matrix[layer][j];
}

/* Save bottom right corner */
tmp2 = Matrix[MatHei-layer-1][MatWid-layer-1];
/* Right bound */
for (i = MatHei-layer-2 ; i >= layer ; i--)
{
if (i != layer)
Matrix[i+1][MatWid-layer-1] = Matrix[i][MatWid-layer-1];
else
Matrix[i+1][MatWid-layer-1] = tmp1;
}

/* Save bottom left corner */
tmp1 = Matrix[MatHei-layer-1][layer];
/* Bottom bound */
for (j = layer ; j < MatWid-layer-1 ; j++)
{
if (j != MatWid-layer-2)
Matrix[MatHei-layer-1][j] = Matrix[MatHei-layer-1][j+1];
else
Matrix[MatHei-layer-1][j] = tmp2;
}

/* Left bound */
for (i=layer; i<MatHei-layer-1; i++)
{
if (i != MatHei-layer-2)
Matrix[i][layer] = Matrix[i+1][layer];
else
Matrix[i][layer] = tmp1;
}
}
}```

4. > actually i made it but i need to do it in a different way... u see..
a) does what you have work?
b) what "different" way?

5. Originally Posted by Salem
> actually i made it but i need to do it in a different way... u see..
a) does what you have work?
b) what "different" way?
a) yes this is what i have done!
b) i need to do it in a deifferent way, like, doin every row and col and and middle row and col once and do it n times.. like u said...
but still i dont know how to do that because if im trying to work on the top row, and try to save a temp value like:
(0,0)(0,1)(0,2)(0,3)
i = 0
for(i = 0 ; i < MatWid ; i++)
temp = (0,1)
(0,0) = (0,1)

what next??? or othe option cause it doesnt work for me...?

6. Originally Posted by Tom Tomas Zahov
a) yes this is what i have done!
b) i need to do it in a deifferent way, like, doin every row and col and and middle row and col once and do it n times.. like u said...
but still i dont know how to do that because if im trying to work on the top row, and try to save a temp value like:
(0,0)(0,1)(0,2)(0,3)
i = 0
for(i = 0 ; i < MatWid ; i++)
temp = (0,1)
(0,0) = (0,1)

what next??? or othe option cause it doesnt work for me...?
Work it out by hand - forget the computer for now! Do it on the kitchen table, with 4 coins or marbles or whatever.

How do you swap them? Do it a few times, and before too long, you'll begin to see the pattern you use to make those swaps.
Code:
```//begin swap
temp = array[0][0];        //save one of the values to be moved
array[0][0]= array[0][1];  //swap the 2nd value into place
array[0][1] = temp;       //place the first value into it's place
//swap complete. Change the second dimension index to variables in the above code,