[C/C++] Kiểm tra 1 cột trong ma trận có giảm dần không

Yêu cầu:
Kiểm tra 1 cột trong ma trận có giảm dần không

Thuật toán:
1. Viết hàm nhập dữ liệu vào ma trận
2. Viết hàm kiểm tra 1 cột trong ma trận có giảm dần hay không isDescendingCol(float A[][100], int nRow, int nCol, int sCol);
– Duyệt các phần tử cột sCol A[iRow][sCol] và kiểm tra các phần tử có giảm dần không?

Code:

/************Descending column in matrix*****************
*Author: vncoding
*Date : 06/04/2019
*****************************************************/

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define N 100
#define M 100

void ImportData(float[][M], int, int, char*);
void PrintMatrix(float[][M], int, int);
bool isDescendingCol(float[][M], int, int, int);
void main()
{
    int nRow, nCol, sCol;
    float A[N][M];
    bool res;

    printf("\nNumber of row: ");
    scanf("%d", &nRow);
    printf("\nNumber of column: ");
    scanf("%d", &nCol);
    ImportData(A, nRow, nCol, "A");
    PrintMatrix(A, nRow, nCol);

    do
    {
        printf("\nSpecified number: ");
        scanf("%d", &sCol);
    } while (sCol <= 0 || sCol > nCol);

    res = isDescendingCol(A, nRow, nCol, sCol);

    if (res)
        printf("\nColumn (%d) is decending\n", sCol);
    else
        printf("\nColumn (%d) is not decending\n", sCol);

    getch();
}

/*************************************************************
* Function      : isDescendingCol()
* Parameter     : A[][M]: input matrix (I)
                  nRow  : row number   (I)
                  nCol  : column number(I)
                  sCol  : specified column(I)
* Return        : bool
* Description   : Check specified column is descending or not
*************************************************************/
bool isDescendingCol(float A[][M], int nRow, int nCol, int sCol)
{
    int iRow;

    for (iRow = 0; iRow < nRow-1; iRow++)
    {
        if (A[iRow][sCol-1] > A[iRow+1][sCol-1])
        {
            continue;
        }
        else
        {
            return false;
        }
    }

    return true;
}


/*************************************************************
* 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(float 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("%f", &Matrix[iRow][iCol]);
        }
}

/*************************************************************
* Function      : PrintMatrix()
* Parameter     : Matrix: input matrix (I)
                  nRow  : row number   (I)
                  nCol  : column number(I)
* Return        : void
* Description   : Display matrix data
*************************************************************/
void PrintMatrix(float Matrix[][M], int nRow, int nCol)
{
    int iRow, iCol;
    printf("\nA = ");
    for (iRow = 0; iRow < nRow; iRow++)
    {
        printf("\n");
        for (iCol = 0; iCol < nCol; iCol++)
        {
            printf("\t%.2f\t", Matrix[iRow][iCol]);
        }
    }
}

Kết quả:

Number of row: 3
Number of column: 4

A[0][0] = -4.5
A[0][1] = 5.7
A[0][2] = 2.3
A[0][3] = 7.8
A[1][0] = 9.9
A[1][1] = 12.1
A[1][2] = 2.3
A[1][3] = -8.9
A[2][0] = 12
A[2][1] = 10
A[2][2] = 7.8
A[2][3] = 5.6

A =
        -4.50           5.70            2.30            7.80
        9.90            12.10           2.30            -8.90
        12.00           10.00           7.80            5.60
Specified number: 2

Column (2) is not decending

Be the first to comment

Leave a Reply