Informatică, întrebare adresată de vladgutenco1, 8 ani în urmă

Fie dat tabloul bidimensional A [1...n,1...m] cu elemente numere reale
Sa se compuna un program (C/C++) care va calcula maximul elementelor negative si suma elementelor pozitive ce se contin in matricea A.

Răspunsuri la întrebare

Răspuns de CRG
1
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    float A[25][25];
    int n, m;
    float max_negative = 1, suma_pozitive = 0;

    cout << "n = "; cin >> n;
    cout << "m = "; cin >> m;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++) {
            cin >> A[i][j];
            if(A[i][j] < 0 && abs(max_negative - 1) < 0.0001)
                max_negative = A[i][j];
        }

    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            if(A[i][j] < 0) {
                if(max_negative < A[i][j])
                    max_negative = A[i][j];
            }
            else
                suma_pozitive += A[i][j];

    if(max_negative == 1)
        cout << "Matricea nu contine elemente negative" << endl;
    else
        cout << "Maximul elementelor negative: " << max_negative << endl;

    cout << "Suma elementelor pozitive: " << suma_pozitive;

    return 0;
}


Alte întrebări interesante