This question asks us to rotate an N by N matrix. Each element in the matrix is a pixel of an image, and represented by 4 bytes. The question did not indicate the length of byte. And I assume the byte is 8-bits, which is right in most cases currently. Exception of the length of byte could be found here. Because each byte is 8 bits, and each pixel is 4 bytes, I use an int32_t to represent each pixel. Anyway, using a structure of chars will improve its portability.
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | /*============================================================================= # Author: Sheng Yu - https://34.145.67.234 # Email : yusheng123 at gmail dot com # Last modified: 2013-03-21 21:05 # Filename: 1.6.c # Description: rotate a matrix by 90 degree =============================================================================*/ #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> void print_matrix(int32_t **matrix, int size){ // Print out the suqare matrix as size*size int index_r = 0, index_c = 0; if(size<=0){ return; } for(index_r = 0; index_r < size; ++index_r){ printf("%d",matrix[index_r][0]); for(index_c = 1; index_c < size; ++index_c){ printf("t%d",matrix[index_r][index_c]); } printf("n"); } return; } int rotate(int32_t **matrix, int size){ // Rotate the matrix in a clockwise 90 degrees int index_outer=0,index_inner=0,bound = 0; int32_t temp; if(size < 1){ return -1; } bound = size / 2; for(index_outer=0; index_outer<bound; ++index_outer){ // Swap the elements in the (index_outer)th layer of matrix for(index_inner=index_outer; index_inner< size-1-index_outer; ++index_inner){ temp = matrix[index_outer][index_inner]; matrix[index_outer][index_inner] = matrix[size-1-index_inner][index_outer]; matrix[size-1-index_inner][index_outer] = matrix[size-1-index_outer][size-1-index_inner]; matrix[size-1-index_outer][size-1-index_inner] = matrix[index_inner][size-1-index_outer]; matrix[index_inner][size-1-index_outer] = temp; } } return 0; } void free_matrix(int32_t **matrix, int size){ // Free the allocated memory int index = 0; if(matrix == NULL){ return; } for(index=0; index<size; ++index){ if(matrix[index] != NULL){ free(matrix[index]); } } free(matrix); return; } int main(int argc, char **argv){ // The matrix must be a square matrix int32_t **matrix = NULL; int size = 0, result=0, index_outer = 0, index_inner=0; // Check and get argument if(argc != 2){ printf("Invalid arguments.n"); printf("Usage: %s matrix_sizen",argv[0]); return 1; } size = atoi(argv[1]); if(size < 1){ printf("Invalid matrix size: %s.n", argv[1]); printf("Usage: %s matrix_sizen",argv[0]); return 2; } // Prepare the test matrix matrix = (int32_t **) malloc(sizeof(int32_t *)*size); if(matrix == NULL){ printf("Fail to allocate memory.n"); return 3; } memset(matrix, 0, sizeof(int32_t *)*size); for(index_outer = 0; index_outer < size; ++index_outer){ matrix[index_outer] = (int32_t *) malloc(sizeof(int32_t)*size); if(matrix[index_outer] == NULL){ printf("Fail to allocate memory.n"); free_matrix(matrix, size); return 3; } for(index_inner=0; index_inner < size; ++index_inner){ matrix[index_outer][index_inner] = index_outer*size + index_inner; } } // Call function to rotate it printf("Before rotation:n"); print_matrix(matrix,size); result = rotate(matrix,size); if(result == 0){ printf("After rotation:n"); print_matrix(matrix,size); } else{ printf("Error in rotation.n"); } free_matrix(matrix, size); return 0; } |