Tính trung bình cppngk các phần tử dương

include<stdio.h>

include<conio.h>

include<math.h>

define MAX 100

void NhapMang(float a[][MAX], int &dong, int &cot)

{

//Nhập số dòng

do

{

printf("\nNhap vao so dong: ");

// Cách tà đạo: scanf("dong =%d",&dong); // Lúc nhập phải viết thêm chữ ( dong = ) ở khung console

scanf("%d",&dong);

if(dong < 1 || dong > MAX)

{

printf("\nSo dong khong hop le. Xin kiem tra lai!");

}

}while(dong < 1 || dong > MAX);

//Nhập số cột

do

{

printf("\nNhap vao so cot: ");

scanf("%d",&cot);

if(cot < 1 || cot > MAX)

{

printf("\nSo cot khong hop le. Xin kiem tra lai!");

}

}while(cot < 1 || cot > MAX);

for(int i = 0; i < dong; i++)

{

for(int j = 0; j < cot; j++)

{

float temp;

printf("\nNhap a[%d][%d] = ", i, j);

scanf("%f", &temp);

a[i][j] = temp;

}

}

}

void XuatMang(float a[][MAX], int dong, int cot)

{

for(int i = 0; i < dong; i++)

{

for(int j = 0; j < cot; j++)

{

printf("%8.3f", a[i][j]);

}

printf("\n\n");

}

}

float TinhTrungBinhCongCacSoDuong(float a[][MAX], int dong, int cot)

{

float trungBinhCong, tong = 0, dem = 0;

int n = dong * cot;

for(int i = 0; i < n; i++)

{

if(a[i / cot][i % cot] > 0)

{

tong += a[i / cot][i % cot];

dem++;

}

}

return trungBinhCong = tong / dem;

}

int main()

{

float a[MAX][MAX];

int dong, cot;

NhapMang(a, dong, cot);

XuatMang(a, dong, cot);

float TBC = TinhTrungBinhCongCacSoDuong(a, dong, cot);

printf("\nTrung binh cong cac so duong trong mang = %f", TBC);

getch();

return 0;

}

Yêu cầu: Tính trung bình cộng các số 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 trung bình cộng các số dương trong ma trận số thực

Code:

/Average of positive 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 AverageElementInMatrix(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);
average = AverageElementInMatrix(A, nRow, nCol);  
if (average == -1)  
    printf("All of elements are negative\n");  
else  
    printf("\nAverage of positive elements in matrix: %f", average);
getch();  
} /*

  • Function : AverageElementInMatrix()
  • Parameter : A[][M]: input matrix (I)
                  nRow  : row number   (I)  
                  nCol  : column number(I)    
  • Return : float
  • Description : Average of positive elements in matrix */ float AverageElementInMatrix(float A[][M], int nRow, int nCol) {
    int iRow, iCol;  
    float average = 1.0;  
    int cnt = 0;  
    for (iRow = 0; iRow < nRow; iRow++)  
    {  
        for (iCol = 0; iCol < nCol; iCol++)  
        {  
            if (A[iRow][iCol] > 0)  
            {  
                average += A[iRow][iCol];  
                cnt++;  
            }       
        }  
    }  
    if (cnt == 0)  
        average = -1.0;  
    else  
        average /= cnt;
    return average;  
    
    } /*
  • 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 A[0][0] = -2.3 A[0][1] = 3.4 A[0][2] = 5.6 A[0][3] = 5 A[1][0] = 23 A[1][1] = 12 A[1][2] = -3 A[1][3] = 10 A[2][0] = 9.2 A[2][1] = -4.5 A[2][2] = 6.5 A[2][3] = 3.6 A =

    -2.30           3.40            5.60            5.00  
    23.00           12.00           -3.00           10.00  
    9.20            -4.50           6.50            3.60  
Average of positive elements in matrix: 8.811110