Advertisement
Gilgamesh858

prodotto_Matriciale

Nov 28th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include "funzioni_Prodotto_Matriciale.h"
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     /* Avendo due array A e B ( lungh m ed n) di interi.
  11.      * Scrivere un algoritmo dove gli array sono pieni e restituire una matrice nxm che contenga
  12.      * il prodotto AxB se in A tutti gli elementi sono distinti oppure la matrice identità.
  13.      *
  14.      * Ideato e Prodotto da Ludovico Trupia
  15.      */
  16.     srand(time(NULL));
  17.     const int MAX_ARRAY = 5;
  18.     int a[MAX_ARRAY], b[MAX_ARRAY], c[MAX_ARRAY][MAX_ARRAY];
  19.  
  20.     for( int i = 0; i < MAX_ARRAY; i++)
  21.     {
  22.         a[i] = rand() % 10 + 1;
  23.         b[i] = rand() % 10 + 1;
  24.  
  25.         cout << endl << a[i] << " " << b[i] << endl;
  26.     }
  27.  
  28.     cout << endl << endl;
  29.  
  30.     for( int i = 0; i < MAX_ARRAY; i++)
  31.     {
  32.         for( int j = i+1; j < MAX_ARRAY; j++)
  33.         {
  34.             if( a[i] == a[j] )
  35.             {
  36.                 cout << "//// Esistono elementi ripetuti in A ////" << endl << endl;
  37.                 matrice_Identita(MAX_ARRAY);
  38.                 return 0;
  39.             }
  40.         }
  41.     }
  42.  
  43.     prodotto_Matriciale( MAX_ARRAY, a, b );
  44.  
  45.  
  46.     return 0;
  47. }
  48.  
  49. ______________________________________________________________________________________________________________
  50.  
  51. Contenuto del file "funzioni_Prodotto_Matriciale.h"
  52.  
  53. using namespace std;
  54.  
  55. #ifndef PRODOTTO_MATRICIALE_H
  56. #define PRODOTTO_MATRICIALE_H
  57.  
  58. #endif // PRODOTTO_MATRICIALE_H
  59.  
  60. void prodotto_Matriciale( const int MAX_ARRAY, int a[], int b[] )
  61. {
  62.     int c[MAX_ARRAY][MAX_ARRAY];
  63.  
  64.     for( int i = 0; i < MAX_ARRAY; i++ )
  65.     {
  66.         for( int j = 0; j < MAX_ARRAY; j++ )
  67.         {
  68.            c[i][j] = a[i] * b[j];
  69.         }
  70.     }
  71.  
  72.     cout << "Prodotto Matriciale!" << endl << endl;
  73.     for( int i = 0; i < MAX_ARRAY; i++ )
  74.     {
  75.         cout << "\t| ";
  76.         for( int j = 0; j < MAX_ARRAY; j++ )
  77.         {
  78.             cout << c[i][j];
  79.             if( c[i][j] >= 10 ) cout << " ";
  80.             else cout << "  ";
  81.  
  82.         }
  83.         cout << "|" << endl;
  84.     }
  85.     cout << endl;
  86. }
  87.  
  88.  
  89. void matrice_Identita( const int MAX_ARRAY)
  90. {
  91.     int identita[MAX_ARRAY][MAX_ARRAY];
  92.  
  93.     for( int i = 0; i < MAX_ARRAY; i++ )
  94.     {
  95.         for( int j = 0; j < MAX_ARRAY; j++ )
  96.         {
  97.             if( i != j ) identita[i][j] = 0;
  98.             else identita[i][j] = 1;
  99.         }
  100.     }
  101.  
  102.     cout << "Matrice Identita'!" << endl << endl;
  103.     for( int i = 0; i < MAX_ARRAY; i++ )
  104.     {
  105.         cout << "\t| ";
  106.         for( int j = 0; j < MAX_ARRAY; j++ )
  107.         {
  108.             cout << identita[i][j] << " ";
  109.         }
  110.         cout << "|" << endl;
  111.     }
  112.     cout << endl;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement