int search(const int arr[], int target, int n) { int i, found = 0, where; /* fou
ID: 3694008 • Letter: I
Question
int search(const int arr[], int target, int n) { int i, found = 0, where; /* found - whether or not target has been found */ /* where- index where target found or NOT_FOUND */ /* Compares each element to target */ i = 0; while (!found && i < n) { if (arr[i] == target) found = 1; else ++i; } /* Returns index of element matching target or NOT_FOUND */ if (found) where = i; else where = NOT_FOUND; return (where); } For the search function, what happens if: a. the last ID stored matches the target? b. several ID’s match the target?
Explanation / Answer
a. the last ID stored matches the target?
Answer-> the last ID means ID stored at position n-1 of the array. Hence function will return n-1
b. several ID’s match the target?
Answer-> Since the while loop is breaking when found=true hence function will return the id of first occurence of target
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.