[C/C++] Đếm số lượng phần tử cực đại trong ma trận số thực

Yêu cầu:
Đếm số lượng phần tử cực đại trong ma trận số thực. Một phần tử được gọi là cực đại khi nó lớn hơn các phần tử xung quanh.

Ví dụ:
2 9 8 3 0
1 5 7 6 7
2 4 6 9 1

“5” có phần tử xung quanh là 1, 9, 7 và 4
“2” có phần tử xung quanh là 1 và 9

Số lượng phần tử cực đại là: 3 (gồm có 9, 9 và 7)

Thuật toán:
1. Viết hàm nhập vào ma trận
2. Viết hàm đếm số lượng cực đại trong ma trận
– Duyệt từng phần tử của ma trận
– So sánh phần tử hiện tại A[iRow][iCol] với các phần tử lân cận A[iRow-1][iCol], A[iRow+1][iCol], A[iRow][iCol-1], A[iRow][iCol+1].
– Nếu A[iRow][iCol] không lớn hơn các phần tử xung quanh, không tăng biến đếm và tiếp tục duyệt các phần tử tiếp theo.
– Nếu A[iRow][iCol] lớn hơn tất cả các phần tử xung quanh, tăng biến đếm và duyệt các phần tử tiếp theo.

Code:

/***********Count maximum elements 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(float[][M], int, int, char*);
void PrintMatrix(float[][M], int, int);
int CntMaxInMatrix(float[][M], int, int);


void main()
{
    int nRow, nCol;
    float average;
    float A[N][M];

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

    ImportData(A, nRow, nCol, "A");
    PrintMatrix(A, nRow, nCol);

    printf("\nThe number of maximum elements in matrix: %d", 
        CntMaxInMatrix(A, nRow, nCol));

    getch();
}

/*************************************************************
* Function      : CntMaxInMatrix()
* Parameter     : A[][M]: input matrix (I)
                  nRow  : row number   (I)
                  nCol  : column number(I)
* Return        : float
* Description   : Count maximum elements in matrix
*************************************************************/
int CntMaxInMatrix(float A[][M], int nRow, int nCol)
{
    int iRow, iCol;
    int cnt = 0;

    for (iRow = 0; iRow < nRow; iRow++)
    {
        for (iCol = 0; iCol < nCol; iCol++)
        {
            // Compare with previous element in row
            if (iRow - 1 >= 0)
            {
                if (A[iRow][iCol] <= A[iRow - 1][iCol])
                    continue;
            }
            // Compare with next element in row
            if (iRow + 1 < nRow)
            {
                if (A[iRow][iCol] <= A[iRow + 1][iCol])
                    continue;
            }
            // Compare with previous element in column
            if (iCol - 1 >= 0)
            {
                if (A[iRow][iCol] <= A[iRow][iCol - 1])
                    continue;
            }
            // Compare with next element in column
            if (iCol + 1 < nCol)
            {
                if (A[iRow][iCol] <= A[iRow][iCol + 1])
                    continue;
            }
            cnt++;
        }
    }

    return cnt;
}

/*************************************************************
* 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] = -7.8
A[0][2] = 8.9
A[0][3] = -23
A[1][0] = 34
A[1][1] = 4.6
A[1][2] = 63.3
A[1][3] = 2.3
A[2][0] = 3.
A[2][1] = 5.6
A[2][2] = 6.7
A[2][3] = 7.8

A =
        4.50            -7.80           8.90            -23.00
        34.00           4.60            63.30           2.30
        3.00            5.60            6.70            7.80    

The number of maximum elements in matrix: 3

Be the first to comment

Leave a Reply