[C/C++] Kiểm tra một hàng trong ma trận có tăng dần hay không

Yêu cầu:
Kiểm tra một hàng trong ma trận có tăng dần hay 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 hàng trong ma trận có tăng dần hay không isAscendingRow(float A[][100], int nRow, int nCol, int sRow);
– Duyệt các phần tử dòng sRow A[sRow][iCol] và kiểm tra các phần tử đang tăng dần không?

Code:

/************Ascending row 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 isAscendingRow(float[][M], int, int, int);
void main()
{
    int nRow, nCol, sRow;
    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", &sRow);
    } while (sRow <= 0 || sRow > nRow);

    res = isAscendingRow(A, nRow, nCol, sRow);

    if (res)
        printf("\nRow (%d) is ascending\n", sRow);
    else
        printf("\nRow (%d) is not ascending\n", sRow);

    getch();
}

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

    for (iCol = 0; iCol < nCol-1; iCol++)
    {
        if (A[sRow-1][iCol] < A[sRow-1][iCol + 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] = 3
A[0][1] = -4.5
A[0][2] = 7.8
A[0][3] = 3.9
A[1][0] = 12.3
A[1][1] = 23.4
A[1][2] = 67.6
A[1][3] = 12.3
A[2][0] = 4.5
A[2][1] = 6.7
A[2][2] = 12.5
A[2][3] = 19.8

A =
        3.00            -4.50           7.80            3.90
        12.30           23.40           67.60           12.30
        4.50            6.70            12.50           19.80
Specified number: 3

Row (3) is ascending

Be the first to comment

Leave a Reply