Advertisement
Gilgamesh858

diagonale_Parallela_Nulla

Dec 17th, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. /*
  8.  * ESERCIZIO
  9.  * Data una matrice quadrata di interi dire se esiste una diagonale parallela alla principale a somma nulla
  10.  *
  11.  * By Trupia Ludovico
  12.  *
  13.  */
  14.  
  15. #define COLONNE 7
  16.  
  17. void matrice_Intera( int matrice[][COLONNE] );
  18. void stampa_Matrice ( int matrice[][COLONNE] );
  19. bool diagonale_Nulla ( int matrice[][COLONNE] );
  20.  
  21. int main()
  22. {
  23.     srand( time ( NULL ));
  24.  
  25.     int intera[COLONNE][COLONNE] ;
  26.  
  27.     cout << endl;
  28.  
  29.     matrice_Intera( intera );
  30.     stampa_Matrice ( intera );
  31.  
  32.     if ( diagonale_Nulla ( intera ) )
  33.     {
  34.         cout << endl << endl << "!!!!! ESISTE la parallela NULLA !!!!! " << endl << endl;
  35.     }
  36.     else
  37.     {
  38.         cout << endl << endl << "!!!!! NON Esiste la parallela NULLA !!!!! " << endl << endl;
  39.     }
  40.  
  41.     return 0;
  42. }
  43.  
  44. void matrice_Intera( int matrice[][COLONNE] )
  45. {
  46.     int n = 0;
  47.  
  48.     for( int i = 0; i < COLONNE; i++)
  49.     {
  50.         for ( int j = 0 ; j < COLONNE ; j++)
  51.         {
  52.             matrice[i][j] = (rand() % 15 +1);
  53.  
  54.             if( i % 2 == 0 )
  55.             {
  56.                 matrice[i][j] = 0 - matrice[i][j];
  57.             }
  58.         }
  59.     }
  60. }
  61.  
  62. void stampa_Matrice ( int matrice[][COLONNE] )
  63. {
  64.     for ( int i = 0 ; i < COLONNE ; i++ )
  65.     {
  66.         cout << endl ;
  67.  
  68.         for ( int j = 0 ; j < COLONNE ; j++ )
  69.         {
  70.             cout << "\t" << matrice[i][j] ;
  71.         }
  72.  
  73.         cout << endl;
  74.     }
  75.     cout << endl;
  76. }
  77.  
  78. bool diagonale_Nulla ( int matrice[][COLONNE] )
  79. {
  80.     int somma = 0;
  81.  
  82.     for ( int k = 0 ; k < COLONNE ; k++ )
  83.     {
  84.         for ( int i = k , j = 0 ; i < COLONNE ; i++ , j++ )
  85.         {
  86.             cout << " i["<<i<<"] j["<<j<<"]" << " \t|| " << matrice [i][j];
  87.             somma += matrice[i][j];
  88.             cout << " \t|| somma -- " << somma << endl;
  89.         }
  90.         cout << endl << endl << endl;
  91.         if ( somma == 0 )
  92.         {
  93.             return true;
  94.         }
  95.         somma = 0;
  96.     }
  97.  
  98.     for ( int k = 0 ; k < COLONNE ; k++ )
  99.     {
  100.         for ( int i = 0 , j = k ; j < COLONNE ; i++ , j++ )
  101.         {
  102.             cout << " i["<<i<<"] j["<<j<<"]" << " \t|| " << matrice [i][j];
  103.             somma += matrice[i][j];
  104.             cout << " \t|| somma -- " << somma << endl;
  105.         }
  106.         cout << endl << endl << endl;
  107.  
  108.         if ( somma == 0 )
  109.         {
  110.             return true;
  111.         }
  112.         somma = 0;
  113.     }
  114.     return false;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement