Final answer:
The function search() is a modified sequential search algorithm that examines all the numbers in the array A[] from A[lo] to A[hi]. It returns the index of the cell that contains the item if found, and -1 if the item is not found. A global variable is incremented before each comparison, and its value is printed after the function call.
Step-by-step explanation:
The function search() is a modified sequential search algorithm that examines all the numbers in the array A[] from A[lo] to A[hi], in order, to find the item. It returns the index of the cell that contains the item if found, and -1 if the item is not found. A global variable is incremented before each comparison, and its value is printed after the function call.
Here is an example implementation of the search() function:
int search(int A[], int lo, int hi, int item) {
int comparisons = 0;
for (int i = lo; i <= hi; i++) {
comparisons++;
if (A[i] == item) {
return i;
}
}
cout << 'Number of comparisons: ' << comparisons << endl;
return -1;
}
In this implementation, the variable 'comparisons' is incremented before each comparison, and its value is printed after the loop is finished.