#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Rotate 2D Array using loops

Hi all, I currently have an assignment where i have to rotate the integers in a 2D Matrix, 1 spot in an anticlockwise direction. I've managed to do this using hard code, however we need to use loops to achieve this (no specific kinds of loops). I'm not sure how to take the code I have within the rotateIn function and transform it so it works using the loops. I was wondering if anyone could help out. The expected outcome for the array used is {2,3,6},{1,5,9},{4,7,8}.

``````int [][] in = {{1,2,3},
{4,5,6},
{7,8,9}};

int tmp,tmp2;

void setup(){
noLoop();
}

void draw(){

rotateIn(in);

}

void rotateIn(int [][] b){

tmp=b;
tmp2=b;

b=tmp;
tmp=b;

b=tmp2;
tmp2=b;

b=tmp;
tmp=b;

b=tmp2;
tmp2=b;

b=tmp;
tmp=b;

b=tmp2;
tmp2=b;

b=tmp;
tmp=b;

b=tmp2;
}
``````
Tagged:

• One way of approaching a problem like this where you need to define an algorithm is, before working on what to tell the computer to do, imagine how you would give someone advice to do it in the general case.

So, imagine you had given someone a 3x3-square card with 123/456/789, and you wanted to explain to them how to copy numbers onto a new blank card. How would you do that in general?

Even more generally, you might try to give instructions that would work no matter what card they were given -- a 3x3 or a 5x5 or 7x7, etc....

• Strange you rotate it around the center

Normally rotating a table would result in

``````369
258
147
``````

(Swap x and y essentially)

• ^ I'm not sure this is true

• Your code is more complex that it need be. So have a look at this implementation and see if you can follow the advive from jeremydouglass

``````int [][] in = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};

void setup() {
noLoop();
printArray(in);
rotateIn(in);
printArray(in);
}

void printArray(int [][] b) {
for (int[] ba : b) {
for (int i : ba) {
print(i + " ");
}
println();
}
println("-------");
}

void rotateIn(int [][] b) {
int tmp = b;
b = b;
b = b;
b = b;
b = b;
b = b;
b = b;
b = b;
b = tmp;
}
``````
• @koogs: two different things: rotate the entire table by 90 degree OR move every slot one further anti-clockwise one step

• @Chrisir -- given that OP has been asked to move things by one position, the ring-based approach is where a general solution could get interesting. There is a solution for turning a 3x3 by 90 degrees that is really a special case -- but that solution won't work for if, for example, you have a 25x25 and want to turn its third ring by 7 positions. In other words, could you build the list used in rotateIn or rotateOut automatically depending on the matrix size? If you can describe the process of building a list of slot addresses that corresponds to a ring in a 2D matrix, then you can easily manipulate that list to rotate the ring, and that second approach would work to turn the outer ring of a 3x3 by two spaces (that is, 90 degrees) as well as for turning the outer ring of a 100x100 matrix 10 space.

• Thank you!