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

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

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

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

Code:

/******************** Rotate 270 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 Rotate270(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');


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

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

    getch();
}

/*************************************************************
* Function      : Rotate270()
* 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 270 degree
*************************************************************/
void Rotate270(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 = aRow - 1; iARow >= 0; iARow--)
        {
            B[iACol][aRow - 1 - 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] = -1
A[0][1] = 0
A[0][2] = 1
A[0][3] = 2

A[1][0] = 3
A[1][1] = 4
A[1][2] = 5
A[1][3] = 6

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

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

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

Be the first to comment

Leave a Reply

Your email address will not be published.

*