Write a function and prototype statement that accepts three parameters. The first two parameters are pointers to 1-D arrays of integers. You may assume that the two arrays have already been declared with initial values and are of equal size but you must show the declaration of the pointer variables. The third parameter is just an empty 1-D array of integers. Your function must contain statements that select all integer values that are equal between the arrays pointed to by the first two pointer parameters and assign those common values to the third array. Hint: Use nested loops.
void pointers(int *arr1, int *arr2, vector <int> arr3);
int main() {
int arr1[num]; int arr2[num];
cout << "Please enter the items of first and second arrays:\n"; for(int i = 0; i < num; ++i) { cout << "arr1[" << i << "] = "; cin >> arr1[i]; cout << "arr2[" << i << "] = "; cin >> arr2[i]; }
int *arr1Pointer = arr1; int *arr2Pointer = arr2;
vector <int> arr3;
pointers(arr1, arr2, arr3);
for(int v : arr3) cout << v << " ";
return 0; }
void pointers(int *arr1, int *arr2, vector <int> arr3) {
bool flag; for(int val = 0; val < num; ++val) { for(int valj = 0; valj < num; ++valj) { if(arr1[val] == arr2[valj]) { flag = true; for(int v : arr3) { if(v == arr1[val]) { flag = false; break; } } if(flag) arr3.push_back(arr1[val]); } } }
Comments
Leave a comment