[C/C++] Dịch xuống xoay vòng các hàng trong ma trận

Yêu cầu:
Dịch xuống xoay vòng các hàng trong ma trận.
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 được dịch xuống xoay vòng
23 44 66 96
12 11 22 30
21 89 22 33
11 29 43 78

Thuật toán:
1,2. Cấp phát 1 mảng 1 chiều để lưu hàng cuối cùng trong ma trận
3. Shift dòng n vào dòng n+1 (n = 0 ~ aRow-1)
4. Gán dòng cuối cùng vào dòng thứ nhất trong ma trận
5. Giải phóng vùng nhớ tạm pTmp

Code:

/********************* Shift row down **********************
*Author: vncoding
*Date  : 14/05/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 ShiftRowDown(int[][M], int, int);


void main()
{
    int aRow, aCol;
    int A[N][M];
    int col1, col2;

    printf("\nNumber of row: ");
    scanf("%d", &aRow);
    printf("\nNumber of column: ");
    scanf("%d", &aCol);

    ImportData(A, aRow, aCol, "A");
    PrintMatrix(A, aRow, aCol, 'A');

    ShiftRowDown(A, aRow, aCol);

    printf("\nShift matrix\n");
    PrintMatrix(A, aRow, aCol, 'P');

    getch();
}

/*************************************************************
* Function      : ShiftRowDown()
* Parameter     : A[][M]: input matrix (I/O)
*                 aRow  : row number of matrix A (I)
*                 aCol  : column number of matrix A (I)
*
* Return        : void
*
* Description   : Shift row down
*************************************************************/
void ShiftRowDown(int A[][M], int aRow, int aCol)
{
    int iARow, iACol;
    int tmp;
    int* pTmp;

    //1. Allocate temporary memory to store latest row
    pTmp = (int*)malloc(aCol * sizeof(int));
    if (!pTmp)
    {
        printf("Error in memory allocation!\n");
        return;
    }
    memset(pTmp, NULL, aCol * sizeof(int));

    //2. Store last row
    for (iACol = 0; iACol < aCol; iACol++)
    {
        pTmp[iACol] = A[aRow - 1][iACol];
    }

    //3. Shift row n to row n+1
    for (iARow = aRow - 1; iARow > 0 ; iARow--)
    {
        for (iACol = 0; iACol < aCol; iACol++)
        {
            A[iARow][iACol] = A[iARow - 1][iACol];
        }
    }

    //4. Assign last row to 1st row
    for (iACol = 0; iACol < aCol; iACol++)
    {
        A[0][iACol] = pTmp[iACol];
    }

    //5. Free memory
    free(pTmp);
   
}

/*************************************************************
* 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: 4
Number of column: 3

A[0][0] = 1
A[0][1] = 2
A[0][2] = 3
A[1][0] = 4
A[1][1] = 5
A[1][2] = 6
A[2][0] = 7
A[2][1] = 8
A[2][2] = 9
A[3][0] = 10
A[3][1] = 11
A[3][2] = 12

A =
        1               2               3
        4               5               6
        7               8               9
        10              11              12

Shift matrix
P =
        10              11              12
        1               2               3
        4               5               6
        7               8               9

Be the first to comment

Leave a Reply

Your email address will not be published.

*