Advertisement
MouseyN1

Suma elementelor pozitive din matrice (functie)

Oct 5th, 2012
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. /*
  2. 20  Se considera o matrice cu n linii si m coloane. Sa se calculeze si sa se afiseze suma
  3. elementelor pozitive din matrice. Calculul sumei se va face cu ajutorul unei proceduri ce primeste
  4. dimensiunile si valorile matricei si returneaza suma.
  5. */
  6.  
  7. #include <iostream>
  8. using namespace std;
  9. void citireMatrice(int a[100][100], int &n, int &m)
  10. {
  11.     cin >> n >> m;
  12.     for(int i = 1; i <= n; i++)
  13.         for(int j = 1; j <= m; j++)
  14.             cin >> a[i][j];
  15. }
  16.  
  17. void afisareMatrice(int a[100][100], int n, int m)
  18. {
  19.     cout << endl;
  20.     for(int i = 1; i <= n; i++) {
  21.         for(int j = 1; j <= m; j++)
  22.             cout << a[i][j] << " ";
  23.         cout << endl;
  24.     }
  25. }
  26.  
  27. int sumaPozitive(int a[100][100], int n, int m)
  28. {
  29.     int S(0);
  30.     for(int i = 1; i <= n; i++)
  31.     {
  32.         for(int j = 1; j <= m; j++)
  33.             if(a[i][j] > 0)
  34.                 S += a[i][j];
  35.     }
  36.     return S;
  37. }
  38.  
  39. int main()
  40. {
  41.     int a[100][100], n, m;
  42.     citireMatrice(a, n, m);
  43.     afisareMatrice(a, n, m);
  44.     cout << endl << "Suma elementelor pozitive este: " << sumaPozitive(a, n, m);
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement