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[0][0];
tmp2=b[1][0];

b[1][0]=tmp;
tmp=b[2][0];

b[2][0]=tmp2;
tmp2=b[2][1];

b[2][1]=tmp;
tmp=b[2][2];

b[2][2]=tmp2;
tmp2=b[1][2];

b[1][2]=tmp;
tmp=b[0][2];

b[0][2]=tmp2;
tmp2=b[0][1];

b[0][1]=tmp;
tmp=b[0][0];

b[0][0]=tmp2;
}
Tagged:

Answers

  • 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[0][0];
      b[0][0] = b[0][1];
      b[0][1] = b[0][2];
      b[0][2] = b[1][2];
      b[1][2] = b[2][2];
      b[2][2] = b[2][1];
      b[2][1] = b[2][0];
      b[2][0] = b[1][0];
      b[1][0] = 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!

Sign In or Register to comment.