Question: 2 (6 marks) Use C/C++ to answer this question. Suppose that A points to an array of positive integers. In order to tell the end

2 (6 marks) Use C/C++ to answer this question. Suppose that A points to an array of positive integers. In order to tell the end of the array, the last element of the array is always -1. Create a function called int *searchArray(int * A, int x), which searches in A for the occurrences of number x. Each time you call the function, it returns a pointer to the next position of x in the array, if x appears next. Or it returns nullptr. For the first call to searchArray, you need to pass in A and x. But for any subsequent calls you only need to pass in A and -1. As an example, suppose that A points to the following array: subscript element 0 10 1 30 2 40 3 50 4 40 5 60 6 50 7 40 8 30 9 20 10 -1 We can call the function searchArray as follows to see the number of 40s in the array. (Of course, this is only for illustration purpose. We can do many other potential operations using this function, such as processing the numbers between two 40s in the array.) int *pos; int count = 0; pos = searchArray(A, 40); while (pos != nullptr) { cout
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
