// Find the common elements of the two Arrays: int * FindCommon (int * a, int * B, int nA, int nB, int & nOut) {int I = 0; int j = 0; vector <int> vec_comm; int * output = NULL; while (I <nA & j <nB) {if (a [I] <B [j]) ++ I; else if (a [I] = B [j]) {vec_comm.push_back (a [I]); cout <a [I] <endl; ++ I; ++ j;} else // a [I]> B [j] ++ j ;} nOut = vec_comm.size (); int nCoun = 0; if (nOut> 0) {output = new int [nOut]; vector <int >:: iterator itorComm; f Or (itorComm = vec_comm.begin (); itorComm! = Vec_comm.end (); itorComm ++) {output [nCoun] = * itorComm; nCoun ++ ;}} return output ;}
Test code:
int main(){int* a= new int[6];int* b= new int[3];a[0]=2;a[1]=3;a[2]=4;a[3]=6;a[4]=8;a[5]=9;b[0]=8;b[1]=9;b[2]=10;int nOut = 0;int* output = FindCommon(a, b, 6, 3, nOut);for (int i=0; i<nOut; i++){cout << output[i] << "," ;}cout << endl;delete[] a;a=NULL;delete[] b;b=NULL;delete[] output;output=NULL;cout << endl;return 0;}