Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

QUESTIONS 1. The code of a function that searches for a target value in an array is given below. Assume the target has not been

QUESTIONS

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
    1. if the current element matches the target
      1. Set a flag to indicate that the target has been found

else

    1. Advance to the next array element.
  1. if the target was found
    1. Return the target index as the search result

else

  1. Return -1 as the search result.

You must copy the following code into editor that is given under the link: https://www.onlinegdb.com/

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

***********************

/* 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Databases With PostgreSQL From Novice To Professional

Authors: Richard Stones, Neil Matthew

2nd Edition

1590594789, 978-1590594780

More Books

Students also viewed these Databases questions