92.4k views
3 votes
Create a user-defined/standalone function that takes three parameters: two same-sized arrays, as well as the array size. The data type of the arrays is templatized, namely, generic data type. The function MUST use pointer arithmetic to compare if the two arrays are the same or not. Return true if same, false otherwise. (Note: You are only required to provide the function definition (with function header). No need to create main() or any test code.)

1 Answer

3 votes

Final answer:

Here is the function definition for a user-defined function that uses pointer arithmetic to compare two same-sized arrays and return true if they are the same.

Step-by-step explanation:

Here is the function definition for the requested user-defined function:

template<typename T>bool compareArrays(T* arr1, T* arr2, int size) {
for (int i = 0; i < size; ++i) {
if (*(arr1 + i) != *(arr2 + i)) {
return false;
}
}
return true;
}

This function takes two same-sized arrays and the array size as parameters. It uses pointer arithmetic to compare the elements of the two arrays. If all elements are equal, the function returns true; otherwise, it returns false.

User PseudoToad
by
9.0k points