Question: ***First, complete the program and later run it. After you thing that the program is executing correctly then paste it into the below box. 1.

***First, complete the program and later run it. After you thing that the program is executing correctly then paste it into the below box.

1. The code of a function that searches for a target value in an array is given below. 1. Assume the target has not been found. 2. Start with the initial array element. 3. repeat while the target is not found and there are more array elements 4. if the current element matches the target 5. Set a flag to indicate that the target has been found else 1. Advance to the next array element. 7. if the target was found 8. Return the target index as the search result else 1. Return -1 as the search result.

/* Figure 7.14 Function That Searches for a Target Value in an Array */

#define NOT_FOUND -1 /* Value returned by search function if target not

found */

/*

* Searches for target item in first n elements of array arr

* Returns index of target or NOT_FOUND

* Pre: target and first n elements of array arr are defined and n>=0

*/

int

search(const int arr[], /* input - array to search */

int target, /* input - value searched for */

int n) /* input - number of elements to search */

{

int i,

found = 0, /* 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);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!