Answer:
#include <assert.h>
void arrayFill(int *p, int n, int v)
{
for (int i = 0; i < n; i++)
{
*p = v;
p++;
}
}
void arrayReverse(int *p, int n)
{
int *q = p + n - 1;
while (p < q)
{
int temp = *p;
*p = *q;
*q = temp;
p++;
q--;
}
}
void arrayCopy(int *p, int n, int *q)
{
for (int i = 0; i < n; i++)
{
*p = *q;
p++;
q++;
}
}
bool arrayEqual(int *p, int n, int *q)
{
for (int i = 0; i < n; i++)
{
if (*p != *q)
{
return false;
}
p++;
q++;
}
return true;
}
Step-by-step explanation:
The code includes 4 utility functions to make operating on arrays in C++ easier:
- arrayFill: This function takes a pointer to an integer (p), the number of elements to be filled (n), and a value to fill the elements with (v). It uses a for loop to iterate through the n elements, starting from the pointer p, and sets each element to the value v.
- arrayReverse: This function takes a pointer to an integer (p) and the number of elements to be reversed (n). It initializes a pointer q to point to the last element of the array (p + n - 1). Then it uses a while loop to iterate through the elements, starting from the first (p) and last (q) elements, swapping the values of each pair of elements until the pointers meet in the middle.
- arrayCopy: This function takes two pointers to integers (p and q) and the number of elements to be copied (n). It uses a for loop to iterate through the n elements, starting from the pointer q, and copies each element to the corresponding location pointed to by p.
- arrayEqual: This function takes two pointers to integers (p and q) and the number of elements to be compared (n). It uses a for loop to iterate through the n elements, starting from the pointers p and q, and compares each pair of elements. If any pair of elements are not equal, the function returns false. If all pairs of elements are equal, the function returns true.