[C/C++] Kiểm tra ma trận số nguyên có tồn tại chữ số lẻ không

Yêu cầu:
Kiểm tra ma trận số nguyên có tồn tại chữ số lẻ không

Thuật toán:
1. Viết hàm nhập dữ liệu vào ma trận nguyên
2. Viết hàm kiểm tra số nguyên có chứa chữ số lẻ (isOddDigits())
3. Viết hàm kiểm tra ma trận có chứa chữ số lẻ không (isOddDigitsInMatrix())
– Duyệt các phần tử trong ma trận
– Gọi hàm isOddDigits(A[iRow][iCol]) để kiểm tra phần tử ma trận

Code:

/************Check odd digits in matrix**************
*Author: vncoding
*Date : 31/03/2019
*****************************************************/

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

void ImportData(int[][M], int, int, char*);
void PrintMatrix(int[][M], int, int);
bool isOddDigits(int);
bool isOddDigitsInMatrix(int[][M], int, int);
void main()
{
    int nRow, nCol;
    int 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);

    res = isOddDigitsInMatrix(A, nRow, nCol);

    if (res)
        printf("\nMatrix A contains odd digit\n");
    else
        printf("\nMatrix A does not contain odd digit\n");

    getch();
}

/*************************************************************
* Function      : isOddDigitsInMatrix()
* Parameter     : A[][M]: input matrix (I)
nRow  : row number   (I)
nCol  : column number(I)
* Return        : bool
* Description   : Sum of odd element in matrix
*************************************************************/
bool isOddDigitsInMatrix(int A[][M], int nRow, int nCol)
{
    int iRow, iCol;
    bool res;
    for (iRow = 0; iRow < nRow; iRow++)
    {
        for (iCol = 0; iCol < nCol; iCol++)
        {
            if (isOddDigits(A[iRow][iCol]))
            {
                return true;
            }
        }
    }
    return false;
}

/*************************************************************
* Function      : isOddDigits()
* Parameter     : n: input integer (I)
* Return        : bool
* Description   : check odd digits
*************************************************************/
bool isOddDigits(int n)
{
    int tmp;

    do
    {
        tmp = n % 10;
        if (tmp % 2 != 0)
        {
            return true;
        }
        n = n / 10;
    } while (n != 0);

    return false;
}

/*************************************************************
* 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)
* Return        : void
* Description   : Display matrix data
*************************************************************/
void PrintMatrix(int 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%d\t", Matrix[iRow][iCol]);
        }
    }
}

Kết quả:

Number of row: 3
Number of column: 4

A[0][0] = 122
A[0][1] = -800
A[0][2] = -468
A[0][3] = 224
A[1][0] = 464
A[1][1] = 888
A[1][2] = 2
A[1][3] = 24
A[2][0] = 42
A[2][1] = 66
A[2][2] = 68
A[2][3] = 86

A =
        122             -800            -468            224
        464             888             2               24
        42              66              68              86
Matrix A contains odd digit

Be the first to comment

Leave a Reply