[C/C++] Hoán vị 2 dòng trên ma trận

Yêu cầu:
Hoán vị 2 dòng trên ma trận

Thuật toán:
Duyệt theo cột và thực hiện đổi chỗ phần tử tương ứng của 2 dòng

Code:

/***************** Permute 2 rows in matrix ****************
*Author: vncoding
*Date  : 13/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 PermuteRow(int[][M], int, int, int, int);


void main()
{
    int aRow, aCol;
    int A[N][M];
    int row1, row2;

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

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

    printf("\nNumber of row1 (0 ~ %d): ", aRow - 1);
    scanf("%d", &row1);
    printf("\nNumber of row2 (0 ~ %d): ", aRow - 1);
    scanf("%d", &row2);

    PermuteRow(A, aRow, aCol, row1, row2);

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

    getch();
}

/*************************************************************
* Function      : PermuteRow()
* Parameter     : A[][M]: input matrix (I/O)
*                 aRow  : row number of matrix A (I)
*                 aCol  : column number of matrix A (I)
*                 row1  : row 1 (I)
*                 row2  : row 2 (I)
* Return        : void
*
* Description   : Permute 2 rows in matrix
*************************************************************/
void PermuteRow(int A[][M], int aRow, int aCol, int row1, int row2)
{
    int iARow, iACol;
    int tmp;

    if (row1 < 0 || row1 >= aRow ||
        row2 < 0 || row2 >= aRow ||
        row1 == row2)
    {
        return;
    }

    // Sweep according to column
    for (iACol = 0; iACol < aCol; iACol++)
    {
        tmp = A[row1][iACol];
        A[row1][iACol] = A[row2][iACol];
        A[row2][iACol] = tmp;
    }
}

/*************************************************************
* 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: 4

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

A =
        1               2               3               4
        5               6               7               8
        9               10              11              12
        13              14              15              16

Number of row1 (0 ~ 3): 0
Number of row2 (0 ~ 3): 3

Permuted matrix
P =
        13              14              15              16
        5               6               7               8
        9               10              11              12
        1               2               3               4

Be the first to comment

Leave a Reply

Your email address will not be published.

*