L
lovecreatesbeauty
I write a function to rotate a matrix by 90 degrees clockwise, this
function works on a matrix of specific size, for example, it rotates a
4*4 matrix of integers in the following code. The function makes one
more copy of the original matrix, how is the efficiency? Can the copy
be saved? Comments on it are welcome.
Can I extend it to work on a matrix of any size (or even any type, not
just integers) for the logic may be the same when it applies to a
matrix of different size?
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
/*Rotate a 4*4 matrix of integers by 90 degrees clockwise.*/
void rotate(void *matrix, const int width){
int (*array)[4] = matrix;
int (*tmp)[4];
int i, j;
size_t n = width * width * sizeof **array;
if(tmp = malloc(n)){
for (i = 0; i < width; ++i)
for (j = 0; j < width; ++j)
*(*(tmp + i) + j) = *(*(array + i) + j);
for (i = 0; i < width; ++i)
for (j = 0; j < width; ++j)
*(*(array + j) + (width - 1 - i)) = *(*(tmp + i) + j);
free(tmp);
}
}
/*test*/
#include <stdio.h>
/*print the content of a matrix*/
void printa(void *matrix, const int width){
int (*array)[4] = matrix;
int i, j;
for (i = 0; i < width; ++i){
for (j = 0; j < width; ++j)
printf("%2d ", *(*(array + i) + j));
printf("\n");
}
}
#define WTH 4
int main(void){
int a[][WTH] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,16}};
printa(a, WTH);
printf("\n");
rotate(a, WTH);
printa(a, WTH);
printf("\n");
rotate(a, WTH);
printa(a, WTH);
printf("\n");
rotate(a, WTH);
printa(a, WTH);
printf("\n");
rotate(a, WTH);
printa(a, WTH);
printf("\n");
return 0;
}
$ cc -ansi -pedantic -Wall -W rotate.c
rotate.c: In function `rotate':
rotate.c:13: warning: suggest parentheses around assignment used as
truth value
$ ./a.out
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
13 9 5 1
14 10 6 2
15 11 7 3
16 12 8 4
16 15 14 13
12 11 10 9
8 7 6 5
4 3 2 1
4 8 12 16
3 7 11 15
2 6 10 14
1 5 9 13
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
$
function works on a matrix of specific size, for example, it rotates a
4*4 matrix of integers in the following code. The function makes one
more copy of the original matrix, how is the efficiency? Can the copy
be saved? Comments on it are welcome.
Can I extend it to work on a matrix of any size (or even any type, not
just integers) for the logic may be the same when it applies to a
matrix of different size?
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
/*Rotate a 4*4 matrix of integers by 90 degrees clockwise.*/
void rotate(void *matrix, const int width){
int (*array)[4] = matrix;
int (*tmp)[4];
int i, j;
size_t n = width * width * sizeof **array;
if(tmp = malloc(n)){
for (i = 0; i < width; ++i)
for (j = 0; j < width; ++j)
*(*(tmp + i) + j) = *(*(array + i) + j);
for (i = 0; i < width; ++i)
for (j = 0; j < width; ++j)
*(*(array + j) + (width - 1 - i)) = *(*(tmp + i) + j);
free(tmp);
}
}
/*test*/
#include <stdio.h>
/*print the content of a matrix*/
void printa(void *matrix, const int width){
int (*array)[4] = matrix;
int i, j;
for (i = 0; i < width; ++i){
for (j = 0; j < width; ++j)
printf("%2d ", *(*(array + i) + j));
printf("\n");
}
}
#define WTH 4
int main(void){
int a[][WTH] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,16}};
printa(a, WTH);
printf("\n");
rotate(a, WTH);
printa(a, WTH);
printf("\n");
rotate(a, WTH);
printa(a, WTH);
printf("\n");
rotate(a, WTH);
printa(a, WTH);
printf("\n");
rotate(a, WTH);
printa(a, WTH);
printf("\n");
return 0;
}
$ cc -ansi -pedantic -Wall -W rotate.c
rotate.c: In function `rotate':
rotate.c:13: warning: suggest parentheses around assignment used as
truth value
$ ./a.out
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
13 9 5 1
14 10 6 2
15 11 7 3
16 12 8 4
16 15 14 13
12 11 10 9
8 7 6 5
4 3 2 1
4 8 12 16
3 7 11 15
2 6 10 14
1 5 9 13
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
$