Advertisement
Emanuele_Bruno

Esercizio 2 del 30/06/2015

Dec 13th, 2015
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. const int N=5;
  5.  
  6. bool diagonale (int A[][N]);
  7.  
  8. int main()
  9. {
  10.     int A[][N]={{0,1,2,3,4},
  11.                 {5,6,7,8,9},
  12.                 {0,1,2,3,4},
  13.                 {5,6,7,8,9},
  14.                 {0,1,-6,3,4}};
  15.     int i=0,k;
  16.     while (i<N)
  17.     {
  18.         k=0;
  19.         while (k<N)
  20.         {
  21.             cout<<A[i][k]<<" ";
  22.             k++;
  23.         }
  24.         cout<<endl;
  25.         i++;
  26.     }
  27.     cout<<"Sto cercando l'elemento opposto nella diagonale destra inferiore: per esempio 8 e -8 oppure 4 e -4\n";
  28.     cout<<"se lo trovo allora stampo true!.... ed ecco a voi ..."<<diagonale(A);
  29.     return 0;
  30. }
  31.  
  32. bool diagonale (int A[][N])
  33. {
  34.     int a,i,b=1,k=0,j,l;
  35.     i=b;
  36.     while (b<(N-1))
  37.     {
  38.         a=A[i][k];
  39.         j=i+1;
  40.         l=k+1;
  41.         while (j<N)
  42.         {//cout<<A[j][l]<<"\n"; // per debug
  43.             if (a==(-(A[j][l]))) return true;
  44.             j++;
  45.             l++;
  46.         }
  47.         k++;
  48.         i++;
  49.         if (i==N-1)
  50.         {
  51.             b++;
  52.             i=b;
  53.             k=0;
  54.         }
  55.     }
  56.     return false;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement