[C/C++] Đếm các giá trị yên ngựa trong ma trận

Yêu cầu:
Đếm các giá trị yên ngựa trong ma trận.
Phần tử “yên ngựa” là phần tử lớn nhất trên hàng và nhỏ nhất trên cột trong ma trận

Ví dụ: cho ma trận
12 32 34 90
1 31 30 5
4 33 20 44
3 34 35 23

Phần tử 31 là phần tử “yên ngựa”

Thuật toán:
1. Viết hàm nhập vào ma trận số thực
2. Viết hàm đếm các giá trị yên ngựa trong ma trận
Duyệt từng hàng của ma trận, tìm giá trị maximum (giả sử tại vị trí (i,j)). Khi đó, duyệt theo cột j để kiểm tra giá trị A[i][j] có phải nhỏ nhất trên cột j không.

Code:

/************Count saddle 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 CntSaddleInMatrix(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("\nCount saddle elements in matrix: %d",
        CntSaddleInMatrix(A, nRow, nCol));

    getch();
}

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

    for (iRow = 0; iRow < nRow; iRow++)
    {
        max = A[iRow][0];
        col = 0;
        for (iCol = 1; iCol < nCol; iCol++)
        {
            // Find maximum in a row
            if (max < A[iRow][iCol])
            {
                max = A[iRow][iCol];
                col = iCol;
            }
        }

        // Find minimum in a column col
        min = A[0][col];
        for (int i = 1; i < nRow; i++)
        {
            if (min > A[i][col])
            {
                min = A[i][col];
            }
        }

        // Compare maximum in row and minimm in column
        if (max == min)
        {
            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: 4
Number of column: 4

A[0][0] = 12
A[0][1] = 32
A[0][2] = 34
A[0][3] = 60
A[1][0] = 1
A[1][1] = 31
A[1][2] = 30
A[1][3] = 5
A[2][0] = 4
A[2][1] = 33
A[2][2] = 20
A[2][3] = 44
A[3][0] = 3
A[3][1] = 34
A[3][2] = 35
A[3][3] = 23

A =
        12.00           32.00           34.00           60.00
        1.00            31.00           30.00           5.00
        4.00            33.00           20.00           44.00
        3.00            34.00           35.00           23.00
Count saddle elements in matrix: 1

Be the first to comment

Leave a Reply