Re: Code review of an matrix rotation function
- From: "lovecreatesbea...@xxxxxxxxx" <lovecreatesbeauty@xxxxxxxxx>
- Date: Sun, 07 Oct 2007 14:50:13 -0700
On Oct 8, 3:31 am, Thad Smith <ThadSm...@xxxxxxx> wrote:
Michal Nazarewicz wrote:
"lovecreatesbea...@xxxxxxxxx" <lovecreatesbea...@xxxxxxxxx> writes:
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.
#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.
Thank you.
The following is the update for my previous code.
#define MTXROT_SIZE 6 /* length or width of the matrix */
void *mtxrot(void *mtx)
{
int (*p)[MTXROT_SIZE] = mtx;
const int M = MTXROT_SIZE % 2 ? MTXROT_SIZE / 2 - 1 : (MTXROT_SIZE -
1) / 2;
const int N = MTXROT_SIZE - 1;
int r, c, i;
for (r = 0; r <= N / 2; r++)
for (c = 0; c <= M; c++){
i = p[r ][c ];
p[r ][c ] = p[N - c][r ];
p[N - c][r ] = p[N - r][N - c];
p[N - r][N - c] = p[c ][N - r];
p[c ][N - r] = i;
}
return p;
}
I adopt your suggestion and come up with the following new code. For
my code only deals with the matrix whose length equals to width, there
is only one number for the length or width in the prototype. This new
one also removes the ugly extra macro in my old code.
/* Rotate an n * n matrix of integers of any size provided its length
equals to
width */
void *mtxrot(int *p, int n)
{
int r, c, i;
const int m = n % 2 ? n / 2 - 1 : (n - 1) / 2;
const int k = n - 1;
for (r = 0; r <= k / 2; r++)
for (c = 0; c <= m; c++){
i = *(p + (n * r) + c);
*(p + (n * r) + c) = *(p + n * (k - c) + r);
*(p + n * (k - c) + r) = *(p + n * (k - r) + k - c);
*(p + n * (k - r) + k - c) = *(p + n * c + k - r);
*(p + n * c + k - r) = i;
}
return p;
}
.
- References:
- Code review of an matrix rotation function
- From: lovecreatesbea...@xxxxxxxxx
- Re: Code review of an matrix rotation function
- From: Michal Nazarewicz
- Re: Code review of an matrix rotation function
- From: Thad Smith
- Code review of an matrix rotation function
- Prev by Date: Re: C programming
- Next by Date: Re: C programming
- Previous by thread: Re: Code review of an matrix rotation function
- Next by thread: Re: Code review of an matrix rotation function
- Index(es):
Relevant Pages
|