[C/C++] Tìm giá trị xuất hiện nhiều nhất trong ma trận số thực

Yêu cầu:
Tìm giá trị xuất hiện nhiều nhất trong ma trận số thực

Thuật toán:
1. Cấp phát 1 mảng struct gồm có 2 trường (arr: lưu giá trị phần tử trong ma trận, cnt: lưu số lần xuất hiện phần tử trong ma trận)

typedef struct arrcnt
{
    float arr;
    int cnt;
}ARR_CNT;

2. Duyệt ma trận để tìm số lần xuất hiện của mỗi phần tử trong ma trận
3. Duyệt mảng struct ARR_CNT để tìm ra phần tử có số lần xuất hiện nhiều nhất (cnt)

Code:

/*************Find the most appeared element **************
*Author: vncoding
*Date : 12/05/2019
************************************************************/

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

void ImportData(float[][M], int, int, char*);
void PrintMatrix(float[][M], int, int, char);
float FindMostElement(float[][M], int, int);

typedef struct arrcnt
{
    float arr;
    int cnt;
}ARR_CNT;


void main()
{
    int aRow, aCol;
    float A[N][M];
    int res;

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

    ImportData(A, aRow, aCol, "A");
    PrintMatrix(A, aRow, aCol, 'A');

    printf("\nThe element with most appreance in matrix: %f", FindMostElement(A, aRow, aCol));

    getch();
}

/*************************************************************
* Function      : FindMostElement()
* Parameter     : A[][M]: input matrix (I)
*                 aRow  : row number of matrix A (I)
*                 aCol  : column number of matrix A (I)
*
* Return        : element with most appreance
*                 
* Description   : Find element with most appreance
*************************************************************/
float FindMostElement(float A[][M], int aRow, int aCol)
{
    int iARow, iACol;
    int cnt = 0;
    int idx = 0;
    int max;
    int maxidx;
    bool flg;
    ARR_CNT *arr;

    //1. Allocate memory
    arr = (ARR_CNT*)malloc(aRow * aCol * sizeof(ARR_CNT));
    if (!arr)
    {
        printf("Error in allocating memory!");
        return 0.0;
    }
    memset(arr, 0x00, aRow * aCol * sizeof(ARR_CNT));

    //2. Count elements in matrix
    for (iARow = 0; iARow < aRow; iARow++)
    {
        for (iACol = 0; iACol < aCol; iACol++)
        {
            if (cnt == 0)
            {
                arr[cnt].arr = A[iARow][iACol];
                arr[cnt].cnt = 1;
                cnt++;
            }
            else
            {
                flg = false;
                for (idx = 0; idx < cnt; idx++)
                {
                    if (A[iARow][iACol] == arr[idx].arr)
                    {
                        arr[idx].cnt++;
                        flg = true;
                    }
                }
                if (!flg)
                {
                    arr[cnt].arr = A[iARow][iACol];
                    arr[cnt].cnt = 1;
                    cnt++;
                }
            }
        }
    }

    //3. Sweep structure ARR_CNT
    max = arr[0].cnt;
    maxidx = 0;
    for (idx = 1; idx < cnt; idx++)
    {
        if (max < arr[idx].cnt)
        {
            max = arr[idx].cnt;
            maxidx = idx;
        }
    }

    return arr[maxidx].arr;

}

/*************************************************************
* 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)
                  name  : name of matrix (I)
* Return        : void
* Description   : Display matrix data
*************************************************************/
void PrintMatrix(float Matrix[][M], int nRow, int nCol, char name)
{
    int iRow, iCol;
    printf("\n%c = ", name);
    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] = 2.1
A[0][1] = -3.4
A[0][2] = -3.5
A[0][3] = 2.10
A[1][0] = -5.6
A[1][1] = 6.8
A[1][2] = 2.1
A[1][3] = 5.6
A[2][0] = 2.1
A[2][1] = 7.9
A[2][2] = 4.5
A[2][3] = 0

A =
        2.10            -3.40           -3.50           2.10
        -5.60           6.80            2.10            5.60
        2.10            7.90            4.50            0.00

The element with most appreance in matrix: 2.100000

Be the first to comment

Leave a Reply