[C/C++] Liệt kê các dòng toàn âm trong ma trận số thực

Yêu cầu:
Liệt kê các dòng toàn âm trong ma trận số thực.
Ví dụ: cho ma trận
-87 -75 -62 -3
12 23 34 -2
-10 -9 -8 -12

Các dòng toàn âm trong ma trận là: 0, 2

Thuật toán:
1. Viết hàm nhập dữ liệu vào ma trận
2. Viết hàm FindNegativeRow()

Code:

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

void ImportData(float[][M], int, int, char*);
void PrintMatrix(float[][M], int, int);
int* FindNegativeRow(float[][M], int, int, int*);
void main()
{
    int nRow, nCol;
    float A[N][M];
    int *res;
    int cnt;
    int idx;


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

    res = FindNegativeRow(A, nRow, nCol, &cnt);

    if (!cnt)
        printf("\nMatrix does not have negative row\n");
    else
    {
        printf("\nNegative row in matrix: ");
        for (idx = 0; idx < cnt; idx++)
        {
            printf("%d, ", res[idx] + 1);
        }
    }

    getch();
}

/*************************************************************
* Function      : FindNegativeRow()
* Parameter     : A[][M]: input matrix (I)
nRow  : row number   (I)
nCol  : column number(I)
sCol  : specified column(I)
cnt   : total number of negative row (O)
* Return        : pointer to array including negative row index
* Description   : Find negative row in matrix
*************************************************************/
int* FindNegativeRow(float A[][M], int nRow, int nCol, int* cnt)
{
    int iRow, iCol;
    int *pArr = NULL;
    int idx;

    *cnt = 0;

    pArr = (int*)malloc(nRow * sizeof(int));
    if (!pArr)
    {
        return NULL;
    }
    memset(pArr, NULL, nRow * sizeof(int));

    for (iRow = 0; iRow < nRow; iRow++)
    {
        idx = iRow;
        for (iCol = 0; iCol < nCol; iCol++)
        {
            if (A[iRow][iCol] >= 0)
            {
                idx = -1;
                break;
            }
        }
        if (idx != -1)
        {
            pArr[(*cnt)++] = idx;
        }
    }

    return pArr;
}


/*************************************************************
* 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: 3

A[0][0] = -2
A[0][1] = -4.5
A[0][2] = -.2
A[1][0] = 0
A[1][1] = -4
A[1][2] = 3
A[2][0] = 2
A[2][1] = -3
A[2][2] = 4

A =
        -2.00           -4.50           -0.20
        0.00            -4.00           3.00
        2.00            -3.00           4.00
Negative row in matrix: 1,

Be the first to comment

Leave a Reply