algorithm - How do you rotate a two dimensional array? -


inspired raymond chen's post, have 4x4 2 dimensional array, write function rotates 90 degrees. raymond links solution in pseudo code, i'd see real world stuff.

[1][2][3][4] [5][6][7][8] [9][0][1][2] [3][4][5][6] 

becomes:

[3][9][5][1] [4][0][6][2] [5][1][7][3] [6][2][8][4] 

update: nick's answer straightforward, there way better n^2? if matrix 10000x10000?

here in c#

int[,] array = new int[4,4] {     { 1,2,3,4 },     { 5,6,7,8 },     { 9,0,1,2 },     { 3,4,5,6 } };  int[,] rotated = rotatematrix(array, 4);  static int[,] rotatematrix(int[,] matrix, int n) {     int[,] ret = new int[n, n];      (int = 0; < n; ++i) {         (int j = 0; j < n; ++j) {             ret[i, j] = matrix[n - j - 1, i];         }     }      return ret; } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -