473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Code review of an matrix rotation function

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 7428

"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrote in message
news:11**********************@g4g2000hsf.googlegro ups.com...
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;
}

I needed a good laugh this morning, you provided it.
Oct 7 '07 #2
On Oct 7, 9:05 pm, "Barry" <barr...@nullpeoplepc.comwrote:
"lovecreatesbea...@gmail.com" <lovecreatesbea...@gmail.comwrote in message

news:11**********************@g4g2000hsf.googlegro ups.com...


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;
}

I needed a good laugh this morning, you provided it.- Hide quoted text -

- Show quoted text -
What's your funny? Can you share with us. If you can't provide
suggestion and improvement to the code, you and your off-topic
bullshit aren't supposed to appear here, got it?

Oct 7 '07 #3
"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrites:
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? 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) {

We'll need to check size though:

if (size>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 +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--
Oct 7 '07 #4
Michal Nazarewicz wrote:
"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrites:
>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.

--
Thad
Oct 7 '07 #5
On Oct 8, 2:15 am, Michal Nazarewicz <min...@tlen.plwrote:
"lovecreatesbea...@gmail.com" <lovecreatesbea...@gmail.comwrites:
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? Why do you pass a void pointer to the function? I don't
get it. What's wrong with:
Thank you.

My code uses a two dimensional array as a matrix, and a fixed number
is required for me to get back the data type of the matrix inside the
function.

Oct 7 '07 #6
On Oct 8, 3:31 am, Thad Smith <ThadSm...@acm.orgwrote:
Michal Nazarewicz wrote:
"lovecreatesbea...@gmail.com" <lovecreatesbea...@gmail.comwrites:
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;
}
Oct 7 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Volodymyr Sadovyy | last post by:
Hi. Can somebody refer me to resource with specified/analyzed/approximated productivity in Java coding and Java code review tasks? Coding productivity is more described in the net, but I didn't...
3
by: Arvie | last post by:
I need some advice guys.. I am proposing that we get someone to do a complete audit/review of our Java application codebase, about 1000 JSPs/Servlets and 100 EJBs. If I get firms to submit...
0
by: gs-code-review-bounces | last post by:
Your mail to 'gs-code-review' with the subject Re: Application Is being held until the list moderator can review it for approval. The reason it is being held: Post by non-member to a...
1
by: djinni | last post by:
It's been a while since I've written anything in C, and I was hoping to get some feedback on the following code. As far as I can tell, the program works fine, but I'm not sure I'm cleaning all the...
24
by: Robin Cole | last post by:
I'd like a code review if anyone has the time. The code implements a basic skip list library for generic use. I use the following header for debug macros: /* public.h - Public declarations and...
3
by: Filippo | last post by:
Hi, In my organization we would like to activate a code review system, in wich a developer have to pass a review from a reviewer before check in the modified files in source safe. Is there any way...
8
by: Klaas Vantournhout | last post by:
Hi all, I'm in need of a matrix of function pointers, and to be honest. No 'nice' solution has been found yet on that big big internet. It is possible to declare a matrix of function pointers...
3
by: raras | last post by:
could somebody help me to write the script of program to multiply 2D matrix using function and pointer? please ... thanx for helping me!
3
by: Chris Winton | last post by:
Hello my code below is returning the wrong value when the MatrixMult function is called in StateEstimate for the value M2. When the code is debugged, with a break point at void MatrixMult() and i...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.