231k views
4 votes
C/C++ does not allow arrays to be manipulated as a whole; each element must be set or accessed individually. To make operating on whole arrays or contiguous portions of arrays easier, write the following utility functions.

void arrayFill(int *p,int n,int v); // fill n elements starting a p with the value v
void arrayReverse(int *p,int n); // reverse the order of n elements starting a p
void arrayCopy(int *p,int n,int *q); // copy n consecutive elements from q to p
bool arrayEqual(int *p,int n,int *q); // iff n consecutive elements of p and q are equal
You will need to write your own test code to make sure your functions work correctly. The follow is an example of how to do this, but it is unlikely to find all errors.
#include
int main()
{
int a[20] = {1,2,3,4,5,6,7};
arrayReverse(a+2,3);
assert(a[2] == 5);
assert(a[4] == 3);
arrayCopy(a+7,6,a+1);
assert(a[12]==7);
arrayFill(a+13,6,-1);
int b[20] = {1,2,5,4,3,6,7,2,5,4,3,6,7,-1,-1,-1,-1,-1,-1};
assert(arrayEqual(a,20,b));
}
NOTE: Your submission may not use the array index operator ([]). You must use pointers.
Submit your functions without main().

User Nina
by
8.5k points

1 Answer

5 votes

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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
User MrEvil
by
8.1k points