Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ESERCIZIO 1:
- Scrivere un metodo che prenda in input una matrice A di dimensione n*n ed un array B di n elementi distinti, e
- restituisca un array C, allocato dinamicamente di dimensione n, che contenga gli elementi corrispondenti ad
- A intersecato B (C non deve contenere intersezioni)
- */
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #define DIM 3
- using namespace std;
- int *arrayIntersezione(int array[], int matrice[][DIM]);
- void push(int array[], int x);
- int main(){
- srand(time(NULL));
- int matrice[DIM][DIM];
- cout << "MATRICE:" << endl;
- for(int i = 0; i < DIM; i++){
- for(int j = 0; j < DIM; j++){
- matrice[i][j] = rand()%10+1;
- cout << matrice[i][j] << " ";
- }
- cout << endl;
- }
- int array[] = {7, 3, 9};
- cout << "\nARRAY B:" << endl;
- for(int i = 0; i < DIM; i++){
- cout << array[i] << " ";
- }
- cout << endl;
- int *C = arrayIntersezione(array, matrice);
- cout << "\nARRAY C:" << endl;
- for(int i = 0; i < DIM; i++){
- cout << C[i] << " ";
- }
- cout << "\n\n";
- }
- int *arrayIntersezione(int array[], int matrice[][DIM]){
- int *C = new int[DIM];
- for(int i = 0; i < DIM; i++){
- C[i] = -1;
- }
- for(int i = 0; i < DIM; i++){
- bool found = false;
- int k = 0;
- while(k < DIM && !found){
- int j = 0;
- while(j < DIM && !found){
- if(array[i] == matrice[k][j]){
- push(C, array[i]);
- found = true;
- }
- j++;
- }
- k++;
- }
- }
- return C;
- }
- void push(int *array, int x){
- static int i = 0;
- array[i++] = x;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement