GIven the following search function declaration, what would be the corresponding
ID: 3843397 • Letter: G
Question
GIven the following search function declaration, what would be the corresponding declaration for a templated search function?
int search(int array[], int start, int target, int size);
//pre: start is > 0, and < size
// the position of the first occurance of the target at or after start is returned, or -1 is returned.
a. template
int search (int array[], int start, T target, int size);
b. template
T search (T array[], T start, T target, T size);
c. template
T search(T array[], int start, T target, int size);
d. template
int search(T array[], T start, T target, T size);
e. all of the above
f. none of the above
Explanation / Answer
Sample Template declaration:
Example for a template:
OUTPUT:
6
10
For the abve question the answer is C
c. template <class T>
T search(T array[], int start, T target, int size);
Justification:
* The search function can return any generic type.(so T search(..))
* the array to search may contain integer or long or .. and similarly target to search can also occur as any return type so T search(T array[], .. , T target,..)
* the size and start should be a integer, no generic type needed for start and size so declared as int ( T search(T array[], int start, T target, T size)
_____________________________________________
However, Option c suits the given problem .. all the options will work without any errors but type casting may needed in some cases.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.