Ho fatto questo ma ho notato se per caso due dei numeri sono 6 e 0 nel primo array e nel secondo array tra i 5 numeri c'è due volte 0 come risultato mi esce 6 0 0 , dovrebbe venirmi invece come risultato 6 e 0 come comuni non capisco come dovrei correggere...
codice:#include <iostream> #include <cmath> #include <ctime> #include <cstdlib> using namespace std; int main(){ int num1[5]; int num2[5]; srand(time(NULL)); cout<<"array 1"<<endl; for (int i = 0; i <5; i++){ num1[i] = rand() % 10; cout<<num1[i]<<endl; } cout<<"array 2"<<endl; for (int j = 0; j < 5; j++){ num2[j] = rand() % 10; cout<<num2[j]<<endl; } cout<<"elementi comuni"<<endl; for (int i = 0; i <5; i++){ for (int j = 0; j <5; j++){ if (num1[i] == num2[j]) { cout<<" "<<num1[i]; } } } }

Rispondi quotando