MouseyN1

Operatii pe triunghiuri (matrice)

May 11th, 2012
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. /* Recapitulare pentru teza
  2. 2. Se citeste n din fisierul numere.txt, iar apoi n^2 numere naturale ce se vor citi din acelasi fisier.
  3. Sa se calculeze
  4.   a) suma celor din triunghiul de sus
  5.   b) media aritmetica din triunghiul de jos
  6.   c) numerele pare din triunghiul din stanga
  7.   d) produsul numerelor din dreapta
  8. */
  9. #include <fstream>
  10. using namespace std;
  11. int main()
  12. {
  13.     int n, i, j, a[100][100], S(0), P(1), nrPare(0), contor(0), Smedie(0);
  14.     ifstream fin("numere.txt");
  15.     fin >> n;
  16.     for(i = 1; i <= n; i++) {
  17.         for(j = 1; j <= n; j++)
  18.             fin >> a[i][j];
  19.     }
  20.     fin.close();
  21.     ofstream fout("output.txt");
  22.     for(i = 1; i <= n; i++) {
  23.         for(j = 1; j <= n; j++)
  24.         {
  25.             if(i < j && i + j < n + 1)
  26.                 S += a[i][j];
  27.             else
  28.                 if(i < j && i + j > n + 1)
  29.                     P *= a[i][j];
  30.             else
  31.                 if(i > j && i + j > n + 1)
  32.                 {
  33.                     contor++;
  34.                     Smedie += a[i][j];
  35.                 }
  36.             else
  37.                 if(i > j && i + j < n + 1)
  38.                     if(a[i][j] % 2 == 0)
  39.                         nrPare++;
  40.         }
  41.     }
  42.     fout << "Suma: " << S << endl;
  43.     fout << "Produsul: " << P << endl;
  44.     fout << "Media aritmetica: " << (float)Smedie / contor << endl;
  45.     fout << "Numere pare: " << nrPare;
  46.     fout.close();
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment