[C/C++] Xoay ma trận 1 góc 180 độ

Yêu cầu:
Xoay ma trận 1 góc 180 độ
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 180 độ
96 66 44 23
78 43 29 11
33 22 89 21
30 22 11 12

Thuật toán:
Duyệt ma trận A và copy các phần tử từ A vào B theo quy luật sau:

B[0][0] = A[3][3]
B[0][1] = A[3][2]
B[0][2] = A[3][1]
B[0][3] = A[3][0]
...
B[3][0] = A[0][3]
B[3][1] = A[0][2]
B[3][2] = A[0][1]
B[3][3] = A[0][0]

Code:

/******************** Rotate 180 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 Rotate180(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');


    Rotate180(A, B, aRow, aCol, &bRow, &bCol);

    printf("\n180 degree rotated matrix\n");
    PrintMatrix(B, bRow, bCol, 'B');

    getch();
}

/*************************************************************
* Function      : Rotate180()
* 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 180 degree
*************************************************************/
void Rotate180(int A[][M], int B[][M], int aRow, int aCol, int* bRow, int* bCol)
{
    int iARow, iACol;

    *bRow = aRow;
    *bCol = aCol;

    for (iARow = aRow - 1; iARow >= 0; iARow--)
    {
        for (iACol = aCol-1; iACol >= 0; iACol--)
        {
            B[aRow - 1 - iARow][aCol - 1 - iACol] = 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] = -2
A[0][1] = 3
A[0][2] = 4
A[0][3] = 5

A[1][0] = 6
A[1][1] = 7
A[1][2] = 8
A[1][3] = 9

A[2][0] = 1
A[2][1] = 0
A[2][2] = -6
A[2][3] = 3

A =
        -2              3               4               5
        6               7               8               9
        1               0               -6              3
180 degree rotated matrix

B =
        3               -6              0               1
        9               8               7               6
        5               4               3               -2

Be the first to comment

Leave a Reply