[C/C++] Hoán vị 2 cột trên ma trận

Yêu cầu:
Hoán vị 2 cột trên ma trận

Thuật toán:
Duyệt theo hàng và thực hiện đổi chỗ 2 cột tương ứng

Code:

/*************** Permute 2 columns 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 PermuteCol(int[][M], int, int, 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');

    printf("\nNumber of column1 (0 ~ %d): ", aCol - 1);
    scanf("%d", &col1);
    printf("\nNumber of column2 (0 ~ %d): ", aCol - 1);
    scanf("%d", &col2);

    PermuteCol(A, aRow, aCol, col1, col2);

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

    getch();
}

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

    if (col1 < 0 || col1 >= aCol ||
        col2 < 0 || col2 >= aCol ||
        col1 == col2)
    {
        return;
    }

    // Sweep according to row
    for (iARow = 0; iARow < aRow; iARow++)
    {
        tmp = A[iARow][col1];
        A[iARow][col1] = A[iARow][col2];
        A[iARow][col2] = 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 column1 (0 ~ 3): 1
Number of column2 (0 ~ 3): 2

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

Be the first to comment

Leave a Reply

Your email address will not be published.

*