Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Learn Mysql The Easy Way A Beginner Friendly Guide

Authors: Kiet Huynh

1st Edition

B0CNY7143T, 979-8869761545

More Books

Students also viewed these Databases questions

Question

Would you rather have higher pay or better benefits? Why?

Answered: 1 week ago