Question: C++ Step 1:Complete the linearSearch() function. Step 2:Do a binary search on the second array which is already sorted. Code: #include using namespace std; int

C++

Step 1:Complete the linearSearch() function.

Step 2:Do a binary search on the second array which is already sorted.

Code:

#include

using namespace std;

int linearSearch(int [], int SIZE, int target);

int main()

{

int const SIZE = 10;

int target = -1;

int arr[SIZE] = {5, 6, 2, 9, 0, 1, 8, 7, 3, 4};

target = 7;

int pos = linearSearch(arr, SIZE, target);

if ( pos >= 0)

cout << "The target " << target << " is at position " << pos <<

"." << endl;

else

cout << "The target " << target << " cannot be found. " << endl;

/*

target = 17;

pos = linearSearch(arr, SIZE, target);

if ( pos >= 0)

cout << "The target " << target << " is at position " << pos <<

"." << endl;

else

cout << "The target " << target << " cannot be found. " << endl;

int arr2[SIZE] = {10, 11, 12, 29, 30, 41, 78, 97, 103, 154};

target = 97;

pos = binarySearch(arr2, SIZE, target);

if ( pos >= 0)

cout << "The target " << target << " is at position " << pos <<

"." << endl;

else

cout << "The target " << target << " cannot be found. " << endl;

target = 27;

pos = binarySearch(arr2, SIZE, target);

if ( pos >= 0)

cout << "The target " << target << " is at position " << pos <<

"." << endl;

else

cout << "The target " << target << " cannot be found. " << endl;

*/

return 0;

}

int linearSearch(int arr[], int SIZE, int target)

{

int index = -1;

/// TO DO: Add your code here

/// If target is found, return the index, otherwise, return -1;

return index;

}

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!