Yêu cầu:
Xoay ma trận 1 góc 90 độ
Ví dụ: cho ma trận
12 11 22 30
21 89 22 33
11 29 43 78
23 44 66 96
Ma trận sau khi xoay 1 góc 90 độ
30 33 78 96
22 22 43 66
11 89 29 44
12 21 11 23
Thuật toán:
– Duyệt ma trận A và copy lần lượt các phần tử ma trận A vào ma trận B
B[0][0] = A[0][3]
B[0][1] = A[1][3]
B[0][2] = A[2][3]
B[0][3] = A[3][3]
…
B[3][0] = A[0][0]
B[3][1] = A[1][0]
B[3][2] = A[2][0]
B[3][3] = A[3][0]
Code:
/******************** Rotate 90 degree ******************** *Author: vncoding *Date : 3/08/2019 ************************************************************/ #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <memory.h> #define N 100 #define M 100 void ImportData(int[][M], int, int, char*); void PrintMatrix(int[][M], int, int, char); void Rotate90(int[][M], int[][M], int, int, int*, int*); void main() { int A[N][M]; int B[N][M]; int aRow, aCol; int bRow, bCol; printf("\nNumber of row: "); scanf("%d", &aRow); printf("\nNumber of column: "); scanf("%d", &aCol); ImportData(A, aRow, aCol, "A"); PrintMatrix(A, aRow, aCol, 'A'); Rotate90(A, B, aRow, aCol, &bRow, &bCol); printf("\n90 degree rotated matrix\n"); PrintMatrix(B, bRow, bCol, 'B'); getch(); } /************************************************************* * Function : Rotate90() * Parameter : A[][M]: input matrix (I) * B[][M]: ouput matrix (O) * aRow : row number of matrix A (I) * aCol : column number of matrix A (I) * bRow : row number of matrix B (O) * bCol : column number of matrix B (O) * Return : void * * Description : Rotate 90 degree *************************************************************/ void Rotate90(int A[][M], int B[][M], int aRow, int aCol, int* bRow, int* bCol) { int iARow, iACol; *bRow = aCol; *bCol = aRow; for (iACol = aCol-1; iACol >= 0; iACol--) { for (iARow = 0; iARow < aRow; iARow++) { B[aCol - 1 - iACol][iARow] = A[iARow][iACol]; } } } /************************************************************* * Function : ImportData() * Parameter : A[][M]: input matrix (I) nRow : row number (I) nCol : column number(I) nameMatrix: matrix name (I) * Return : void * Description : Import matrix data *************************************************************/ void ImportData(int Matrix[][M], int nRow, int nCol, char* nameMatrix) { int iRow, iCol; for (iRow = 0; iRow < nRow; iRow++) for (iCol = 0; iCol < nCol; iCol++) { printf("\n%s[%d][%d] = ", nameMatrix, iRow, iCol); scanf("%d", &Matrix[iRow][iCol]); } } /************************************************************* * Function : PrintMatrix() * Parameter : Matrix: input matrix (I) nRow : row number (I) nCol : column number(I) name : name of matrix (I) * Return : void * Description : Display matrix data *************************************************************/ void PrintMatrix(int Matrix[][M], int nRow, int nCol, char name) { int iRow, iCol; printf("\n%c = ", name); for (iRow = 0; iRow < nRow; iRow++) { printf("\n"); for (iCol = 0; iCol < nCol; iCol++) { printf("\t%d\t", Matrix[iRow][iCol]); } } }
Kết quả:
Number of row: 3 Number of column: 4 A[0][0] = -12 A[0][1] = 10 A[0][2] = 9 A[0][3] = 3 A[1][0] = 8 A[1][1] = -5 A[1][2] = 9 A[1][3] = 11 A[2][0] = 9 A[2][1] = 6 A[2][2] = 7 A[2][3] = 9 A = -12 10 9 3 8 -5 9 11 9 6 7 9 90 degree rotated matrix B = 3 11 9 9 9 7 10 -5 6 -12 8 9
Leave a Reply
You must be logged in to post a comment.