[C/C++] Tính tổng các phần tử cực trị trong ma trận số thực

Yêu cầu:
Tính tổng các phần tử cực trị trong ma trận số thực.
Một phần tử được gọi là cực trị khi nó lớn hơn các phần tử xung quanh hoặc nhỏ hơn các phần tử xung quanh.

Thuật toán:
1. Viết hàm nhập giá trị vào ma trận
2. Viết hàm đếm phần tử cực trị trong ma trận
– Duyệt từng phần của ma trận
– So sánh phần tử hiện tại A[iRow][iCol] với phần tử lân cận A[iRow-1][iCol], A[iRow+1][iCol], A[iRow][iCol-1], A[iRow][iCol+1] – 2.1 So sánh với phần tử trước đó cùng trên 1 cột
+ Nếu A[iRow][iCol] > A[iRow-1][iCol] => gán flg = 1
+ Nếu A[iRow][iCol] < A[iRow-1][iCol] => gán flg = 2
+ Nếu A[iRow][iCol] == A[iRow-1][iCol] => duyệt phần tử tiếp theo
– 2.2 So sánh với phần tử kế tiếp trên cùng 1 cột và check flg
+ Nếu A[iRow][iCol] > A[iRow+1][iCol] và (flg == 1 hoặc flg == 0) => gán flg = 1
+ Nếu A[iRow][iCol] < A[iRow+1][iCol] và (flg == 1 hoặc flg == 0) => gán flg = 2
+ Nếu A[iRow][iCol] == A[iRow+1][iCol] => duyệt phần tử tiếp theo

– Nếu flg = 1 hoặc flg = 2, cộng dồn giá trị cực trị vào biến sum

Code:

/******Sum minimum/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);
float SumMinMaxInMatrix(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("\nSum minimum/maximum elements in matrix: %f",
        SumMinMaxInMatrix(A, nRow, nCol));

    getch();
}

/*************************************************************
* Function      : SumMinMaxInMatrix()
* Parameter     : A[][M]: input matrix (I)
nRow  : row number   (I)
nCol  : column number(I)
* Return        : float
* Description   : sum minum/maximum elements in matrix
*************************************************************/
float SumMinMaxInMatrix(float A[][M], int nRow, int nCol)
{
    int iRow, iCol;
    float sum = 0.0;
    int flg = 0; // 0: init, 1: maximum, 2: minimum

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

            }
            // Compare with next element in row
            if (iCol + 1 < nCol)
            {
                if (A[iRow][iCol] > A[iRow][iCol + 1] && (flg == 1 || flg == 0))
                    flg = 1;
                else if (A[iRow][iCol] < A[iRow][iCol + 1] && (flg == 2 || flg == 0))
                    flg = 2;
                else
                    continue;
            }
            
            if (flg == 1 || flg == 2)
                sum += A[iRow][iCol];
            
        }
    }

    return sum;
}

/*************************************************************
* 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] = -3.4
A[0][1] = 2.3
A[0][2] = 4.6
A[0][3] = 12
A[1][0] = 3
A[1][1] = 7.6
A[1][2] = -11
A[1][3] = 21
A[2][0] = 0.4
A[2][1] = 3.6
A[2][2] = 1.9
A[2][3] = 5.9
A[3][0] = 11.3
A[3][1] = 4.5
A[3][2] = 6.8
A[3][3] = 7.8

A =
        -3.40           2.30            4.60            12.00
        3.00            7.60            -11.00          21.00
        0.40            3.60            1.90            5.90
        11.30           4.50            6.80            7.80
Sum minimum/maximum elements in matrix: 33.700001

Be the first to comment

Leave a Reply