[C/C++] Xóa 1 cột trong ma trận

Yêu cầu:
Xóa 1 cột 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 sau khi xóa cột số 1
11 22 30
89 22 33
29 43 78
44 66 96

Thuật toán:
Duyệt ma trận A và copy từng phần tử từ A sang B ( trừ các phần tử cột cần xóa)

Code:

/***************** Delete column in matrix  ****************
*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 DelCol(int[][M], int[][M], int, int, int, int*, int*);


void main()
{
    int A[N][M];
    int B[N][M];
    int aRow, aCol;
    int bRow, bCol;
    int iCol;

    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 deleted row: ");
    scanf("%d", &iCol);

    DelCol(A, B, aRow, aCol, iCol, &bRow, &bCol);

    printf("\nNew matrix\n");
    PrintMatrix(B, bRow, bCol, 'B');

    getch();
}

/*************************************************************
* Function      : DelCol()
* 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)
*                 iCol  : specified delete column (I)
*                 bRow  : row number of matrix B (O)
*                 bCol  : column number of matrix B (O)
* Return        : void
*
* Description   : Delete column in matrix
*************************************************************/
void DelCol(int A[][M], int B[][M], int aRow, int aCol, int iCol, int* bRow, int* bCol)
{
    int iARow, iACol;
    int j = 0;

    *bRow = aRow;
    *bCol = aCol-1;

    for (iARow = 0; iARow < aRow; iARow++)
    {
        for (iACol = 0; iACol < aCol; iACol++)
        {
            if (iACol != iCol)
            {
                B[iARow][j++] = A[iARow][iACol];
            }
        }
        j = 0;
    }
}

/*************************************************************
* 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] = 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] = 0
A[2][2] = 1
A[2][3] = 2

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

Number of deleted row: 0

New matrix

B =
        2               3               4
        6               7               8
        0               1               2

Be the first to comment

Leave a Reply

Your email address will not be published.

*