Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #include "funzioni_Prodotto_Matriciale.h"
- using namespace std;
- int main()
- {
- /* Avendo due array A e B ( lungh m ed n) di interi.
- * Scrivere un algoritmo dove gli array sono pieni e restituire una matrice nxm che contenga
- * il prodotto AxB se in A tutti gli elementi sono distinti oppure la matrice identità.
- *
- * Ideato e Prodotto da Ludovico Trupia
- */
- srand(time(NULL));
- const int MAX_ARRAY = 5;
- int a[MAX_ARRAY], b[MAX_ARRAY], c[MAX_ARRAY][MAX_ARRAY];
- for( int i = 0; i < MAX_ARRAY; i++)
- {
- a[i] = rand() % 10 + 1;
- b[i] = rand() % 10 + 1;
- cout << endl << a[i] << " " << b[i] << endl;
- }
- cout << endl << endl;
- for( int i = 0; i < MAX_ARRAY; i++)
- {
- for( int j = i+1; j < MAX_ARRAY; j++)
- {
- if( a[i] == a[j] )
- {
- cout << "//// Esistono elementi ripetuti in A ////" << endl << endl;
- matrice_Identita(MAX_ARRAY);
- return 0;
- }
- }
- }
- prodotto_Matriciale( MAX_ARRAY, a, b );
- return 0;
- }
- ______________________________________________________________________________________________________________
- Contenuto del file "funzioni_Prodotto_Matriciale.h"
- using namespace std;
- #ifndef PRODOTTO_MATRICIALE_H
- #define PRODOTTO_MATRICIALE_H
- #endif // PRODOTTO_MATRICIALE_H
- void prodotto_Matriciale( const int MAX_ARRAY, int a[], int b[] )
- {
- int c[MAX_ARRAY][MAX_ARRAY];
- for( int i = 0; i < MAX_ARRAY; i++ )
- {
- for( int j = 0; j < MAX_ARRAY; j++ )
- {
- c[i][j] = a[i] * b[j];
- }
- }
- cout << "Prodotto Matriciale!" << endl << endl;
- for( int i = 0; i < MAX_ARRAY; i++ )
- {
- cout << "\t| ";
- for( int j = 0; j < MAX_ARRAY; j++ )
- {
- cout << c[i][j];
- if( c[i][j] >= 10 ) cout << " ";
- else cout << " ";
- }
- cout << "|" << endl;
- }
- cout << endl;
- }
- void matrice_Identita( const int MAX_ARRAY)
- {
- int identita[MAX_ARRAY][MAX_ARRAY];
- for( int i = 0; i < MAX_ARRAY; i++ )
- {
- for( int j = 0; j < MAX_ARRAY; j++ )
- {
- if( i != j ) identita[i][j] = 0;
- else identita[i][j] = 1;
- }
- }
- cout << "Matrice Identita'!" << endl << endl;
- for( int i = 0; i < MAX_ARRAY; i++ )
- {
- cout << "\t| ";
- for( int j = 0; j < MAX_ARRAY; j++ )
- {
- cout << identita[i][j] << " ";
- }
- cout << "|" << endl;
- }
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement