Write a function getNeighbors which will accept an integer array, size of the array and an index as parameters. This function will return a new array of size 2 which stores the neighbors of the value at index in the original array. If this function would result in returning garbage values the new array should be set to values {0,0} instead of values from the array.

icon
Related questions
Question
Write a function `getNeighbors` which will accept an integer array, size of the array, and an index as parameters. This function will return a new array of size 2 which stores the neighbors of the value at index in the original array. If this function would result in returning garbage values, the new array should be set to values {0,0} instead of values from the array.
Transcribed Image Text:Write a function `getNeighbors` which will accept an integer array, size of the array, and an index as parameters. This function will return a new array of size 2 which stores the neighbors of the value at index in the original array. If this function would result in returning garbage values, the new array should be set to values {0,0} instead of values from the array.
```cpp
int main() {
    std::cout << std::endl;
    
    const int SIZE = 5;
    int a[SIZE] = {1,2,3,4,5};
    
    int *b = getNeighbors(a, SIZE, 3);
    print(b, 2);
    
    int *c = getNeighbors(a, SIZE, 0);
    print(c, 2);
    
    std::cout << std::endl;
    return 0;
}
```

**Example Output**

```
3 5
0 0
```

### Explanation

This C++ code snippet demonstrates the concept of retrieving neighboring elements from an array. The function `getNeighbors` is designed to return a pointer to the neighbor values of a given element. 

- The array `a` is initialized with the values `{1,2,3,4,5}`.
- `getNeighbors(a, SIZE, 3)` retrieves neighbors of the element at index 3.
- `print(b, 2)` prints the retrieved neighbors, leading to the output `3 5`.
- `getNeighbors(a, SIZE, 0)` retrieves neighbors of the element at index 0.
- `print(c, 2)` prints these neighbors, resulting in `0 0` since there are no valid neighbors.

This specific implementation of the function `getNeighbors` is not shown, but the outputs suggest that it handles out-of-bounds indices by returning zeros.
Transcribed Image Text:```cpp int main() { std::cout << std::endl; const int SIZE = 5; int a[SIZE] = {1,2,3,4,5}; int *b = getNeighbors(a, SIZE, 3); print(b, 2); int *c = getNeighbors(a, SIZE, 0); print(c, 2); std::cout << std::endl; return 0; } ``` **Example Output** ``` 3 5 0 0 ``` ### Explanation This C++ code snippet demonstrates the concept of retrieving neighboring elements from an array. The function `getNeighbors` is designed to return a pointer to the neighbor values of a given element. - The array `a` is initialized with the values `{1,2,3,4,5}`. - `getNeighbors(a, SIZE, 3)` retrieves neighbors of the element at index 3. - `print(b, 2)` prints the retrieved neighbors, leading to the output `3 5`. - `getNeighbors(a, SIZE, 0)` retrieves neighbors of the element at index 0. - `print(c, 2)` prints these neighbors, resulting in `0 0` since there are no valid neighbors. This specific implementation of the function `getNeighbors` is not shown, but the outputs suggest that it handles out-of-bounds indices by returning zeros.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Similar questions