[C/C++] Xóa 1 dòng trong ma trận

Yêu cầu:
Xóa 1 dò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

Kết quả xóa dòng 1
21 89 22 33
11 29 43 78
23 44 66 96

Thuật toán:
Duyệt ma trận A, copy lần lượt các phần tử từ A sang B (ngoại trừ phần tử thuộc dòng cần xóa)

Code:

/***************** Delete row 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 DelRow(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 iRow;

    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", &iRow);

    DelRow(A, B, aRow, aCol, iRow, &bRow, &bCol);

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

    getch();
}

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

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

    for (iARow = 0; iARow < aRow; iARow++)
    {
        if (iARow == iRow)
        {
            continue;
        }
        for (iACol = 0; iACol < aCol; iACol++)
        {
            B[i][iACol] = A[iARow][iACol];
        }
        i++;
    }
}

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

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

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

A[3][0] = 12
A[3][1] = 13
A[3][2] = 14
A[3][3] = 15

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

Number of deleted row: 1

New matrix
B =
        0               1               2               3
        8               9               10              11
        12              13              14              15

Be the first to comment

Leave a Reply

Your email address will not be published.

*