/*
* Return 1 if ptr points to an element within the specified intArray, 0 otherwise.
* Pointing anywhere in the array is fair game, ptr does not have to
* point to the beginning of an element. Check the spec for examples if you are
* confused about what this method is determining.
* size is the size of intArray in number of ints. Can assume size != 0.
* Operators / and % and loops are NOT allowed.
*
* ALLOWED:
* Pointer operators: *, &
* Binary integer operators: -, +, *, <<, >>, ==, ^
* Unary integer operators: !, ~
* Shorthand operators based on the above: ex. <<=, *=, ++, --, etc.
*
* DISALLOWED:
* Pointer operators: [] (Array Indexing Operator)
* Binary integer operators: &, &&, |, ||, <, >, !=, /, %
* Unary integer operators: -
*/
int withinArray(int * intArray, int size, int * ptr) {

}

Respuesta :

Answer:

C++.

Explanation:

Dynamic array consists of a pointer that, by default, points to the first element of the array. If the array name is foo, its first element's value can be accessed through foo[0] or *foo and foo[1] or *(foo + 1) and so on.

To find out if pointer ptr is pointing to any element of the intArray, we need to access each array element's address. We can do that by removing the asterisk from the element i.e. (foo + 1).

////////////////////////////////////////////////////////////////////////////////////

int withinArray(int * intArray, int size, int * ptr) {

    for (int i = 0; i < size; i++) {

       if (ptr == (intArray + i)) {

           return 1;

       }

   return 0;

}