[C/C++] Tính tổng các giá trị trên 1 dòng trong ma trận số thực

Yêu cầu:
Tính tổng các giá trị trên 1 dòng trong ma trận số thực

Thuật toán:
1. Viết hàm nhập vào ma trận số thực
2. Viết hàm tính tổng các giá trị trên 1 dòng trong ma trận

Code:

/*****Sum all of elements in specified row 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 SumRowInMatrix(float[][M], int, int, int);
void main()
{
    int nRow, nCol;
    int iRow;
    float A[N][M];

    printf("\nNumber of row: ");
    scanf("%d", &nRow);
    printf("\nNumber of column: ");
    scanf("%d", &nCol);
    do
    {
        printf("\nNumber of specified row: ");
        scanf("%d", &iRow);
    } while (iRow <= 0 || iRow > nRow);
    

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

    printf("\nSum all of elements in row (%d) matrix: %f", 
            iRow, 
            SumRowInMatrix(A, nRow, nCol, iRow));

    getch();
}

/*************************************************************
* Function      : SumRowInMatrix()
* Parameter     : A[][M]: input matrix (I)
                  nRow  : row number   (I)
                  nCol  : column number(I)  
                  specRow  : specified row(I)
* Return        : int
* Description   : Sum of all elements in row of matrix
*************************************************************/
float SumRowInMatrix(float A[][M], int nRow, int nCol, int specRow)
{
    int iRow, iCol;
    float sum = 0;
    for (iRow = 0; iRow < nRow; iRow++)
    {
        for (iCol = 0; iCol < nCol; iCol++)
        {
            if ((iRow + 1) == specRow)
                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: 3
Number of column: 4
Number of specified row: 1

A[0][0] = 2.3
A[0][1] = 4.5
A[0][2] = -4.5
A[0][3] = 9
A[1][0] = 34
A[1][1] = -6.7
A[1][2] = -4.5
A[1][3] = 8.9
A[2][0] = 2.3
A[2][1] = 12.2
A[2][2] = -3.4
A[2][3] = 5.6

A =
2.30 4.50 -4.50 9.00
34.00 -6.70 -4.50 8.90
2.30 12.20 -3.40 5.60

Sum all of elements in row (1) matrix: 11.300000

Be the first to comment

Leave a Reply