ToniDev

Suma numerelor impare de pe conturul matricei

Oct 23rd, 2023
894
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int afisareConturMatrice(int matrice[][5], int n)
  5. {
  6.     int suma = 0;
  7.  
  8.     for (int i = 0; i < n; i++)
  9.     {
  10.         for (int j = 0; j < n; j++)
  11.         {
  12.             if (i == 0) {
  13.                 if (matrice[i][j] % 2 != 0)
  14.                     suma += matrice[i][j];
  15.             }
  16.             else if (i == n - 1) {
  17.                 if (matrice[i][j] % 2 != 0)
  18.                     suma += matrice[i][j];
  19.             }
  20.             else if (j == 0) {
  21.                 if (matrice[i][j] % 2 != 0)
  22.                     suma += matrice[i][j];
  23.             }
  24.             else if (j == n - 1) {
  25.                 if (matrice[i][j] % 2 != 0)
  26.                     suma += matrice[i][j];
  27.             }
  28.         }
  29.     }
  30.  
  31.     return suma;
  32. }
  33.  
  34. void afisareMatrice(int matrice[][5], int n)
  35. {
  36.     for (int i = 0; i < n; i++)
  37.     {
  38.         for (int j = 0; j < n; j++)
  39.         {
  40.             cout << matrice[i][j] << " ";
  41.         }
  42.         cout << endl;
  43.     }
  44. }
  45.  
  46.  
  47. int main()
  48. {
  49.     int matrice[5][5] = {
  50.         { 1, 1, 1, 1, 1 },
  51.         { 1, 7, 8, 9, 1 },
  52.         { 1, 12, 13, 14, 1 },
  53.         { 1, 17, 18, 19, 1 },
  54.         { 1, 1, 1, 1, 1 }
  55.     };
  56.  
  57.     int n = 5;
  58.  
  59.     afisareMatrice(matrice, n);
  60.  
  61.     cout << endl;
  62.  
  63.     cout << "Suma: " << afisareConturMatrice(matrice, n) << endl;
  64.  
  65.     return 0;
  66. }
  67.  
  68.  
Advertisement
Add Comment
Please, Sign In to add comment