435,491 Members | 3,273 Online
Need help? Post your question and get tips & solutions from a community of 435,491 IT Pros & Developers. It's quick & easy.

# Code review of an matrix rotation function

 P: n/a Hello experts, I code an function to rotate a matrix by 90 degrees clockwise. The matrix can be in any size provided its length equals to width. The one minor limitation is that this requires an extra external macro definition MTXROT_SIZE. Your comments are welcome on this code. Thank you for your time. / ************************************************** ***************************** * Rotate an matrix of any size by 90 degrees colckwise if its length equals to * width, for example: ************************************************** *****************************/ #define MTXROT_SIZE 5 /* length or width of the matrix */ void *mtxrot(void *mtx) { int (*p)[MTXROT_SIZE] = mtx; const int INDMAX = MTXROT_SIZE - 1; const int SCMAX = MTXROT_SIZE % 2 ? MTXROT_SIZE / 2 - 1 : INDMAX / 2; int r, c, i; for (r = 0; r <= INDMAX / 2; r++) for (c = 0; c <= SCMAX; c++){ i = p[r][c]; p[r ][c ] = p[INDMAX - c][r ]; p[INDMAX - c][r ] = p[INDMAX - r][INDMAX - c]; p[INDMAX - r][INDMAX - c] = p[c ][INDMAX - r]; p[c ][INDMAX - r] = i; } return p; } for example, for a 5 x 5 matrix: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 21 16 11 6 1 22 17 12 7 2 23 18 13 8 3 24 19 14 9 4 25 20 15 10 5 for an 6 x 6 matrix: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 31 25 19 13 7 1 32 26 20 14 8 2 33 27 21 15 9 3 34 28 22 16 10 4 35 29 23 17 11 5 36 30 24 18 12 6 Oct 7 '07 #1
6 Replies

 P: n/a "lovecreatesbea...@gmail.com"

 P: n/a On Oct 7, 9:05 pm, "Barry"

 P: n/a "lovecreatesbea...@gmail.com" 1) { const int INDMAX = MTXROT_SIZE - 1; const size_t INDMAX = size - 1; const int SCMAX = MTXROT_SIZE % 2 ? MTXROT_SIZE / 2 - 1 : INDMAX / 2; What's the use of "?:" here? const size_t SCMAX = (size >1) - 1; int r, c, i; size_t r, c; for (r = 0; r <= INDMAX / 2; r++) for (c = 0; c <= SCMAX; c++){ Personally, I prefer: for (r = 0; r <= INDMAX >1; ++r) for (c = 0; c <= SCMAX; ++c){ i = p[r][c]; const int i = mtx[r][c]; p[r ][c ] = p[INDMAX - c][r ]; p[INDMAX - c][r ] = p[INDMAX - r][INDMAX - c]; p[INDMAX - r][INDMAX - c] = p[c ][INDMAX - r]; p[c ][INDMAX - r] = i; } mtx[r ][c ] = mtx[INDMAX - c][r ]; mtx[INDMAX - c][r ] = mtx[INDMAX - r][INDMAX - c]; mtx[INDMAX - r][INDMAX - c] = mtx[c ][INDMAX - r]; mtx[c ][INDMAX - r] = i; } Didn't check the algorithm though. return p; } } return mtx; } -- Best regards, _ _ .o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o ..o | Computer Science, Michal "mina86" Nazarewicz (o o) ooo +------ooO--(_)--Ooo-- Oct 7 '07 #4

 P: n/a Michal Nazarewicz wrote: "lovecreatesbea...@gmail.com" I code an function to rotate a matrix by 90 degrees clockwise. Thematrix can be in any size provided its length equals to width. The oneminor limitation is that this requires an extra external macrodefinition MTXROT_SIZE. Your comments are welcome on this code. Thankyou for your time. >#define MTXROT_SIZE 5 /* length or width of the matrix */void *mtxrot(void *mtx){ int (*p)[MTXROT_SIZE] = mtx; Why the macro? A fixed size is easier than a variable size. Why do you pass a void pointer to the function? I don't get it. What's wrong with: int **mtxrot(int *const *const mtx, const size_t size) { The problem with that definition is that a matrix is usually declared as a two-dimensional array, which your parameter is not compatible with. My choice is void MatrixRotate(int *m, size_t size, size_t ncols) I then need to map two-dimensional indexing to single dimension. There is a separate parameter for ncols in case the working matrix is smaller than the declared array dimensions. -- Thad Oct 7 '07 #5

 P: n/a On Oct 8, 2:15 am, Michal Nazarewicz

 P: n/a On Oct 8, 3:31 am, Thad Smith

### This discussion thread is closed

Replies have been disabled for this discussion.