(Linear Search) Modify the program in Fig. 6.15 to use recursive function linearSearch to perform a linear search of the array. The function should receive an integer array and the size of the array as arguments. If the search key is found, return the array subscript; otherwise, return –1.

What will be an ideal response?


```
#include
using namespace std;

int linearSearch( const int [], int, int, int );

int main()
{
const int SIZE = 100;
int array[ SIZE ];
int searchKey;
int element;

// initialize array elements
for ( int loop = 0; loop < SIZE; loop++ )
array[ loop ] = 2 * loop;

// obtain search key from user
cout << "Enter the integer search key: ";
cin >> searchKey;

// search array for search key
element = linearSearch( array, searchKey, 0, SIZE - 1 );

// display if search key was found
if ( element != -1 )
cout << "Found value in element " << element << endl;
else
cout << "Value not found" << endl;
} // end main

// function to search array for specified key
int linearSearch( const int array[], int key, int low, int high )
{
// search array for key
if ( array[low] == key )
return low;
else if ( low == high )
return -1;
else
return linearSearch( array, key, low + 1, high );
} // end function linearSearch
```
Enter the integer search key: 18
Found value in element 9
Enter the integer search key: 17
Value not found

Computer Science & Information Technology

You might also like to view...

Which of the following is not one of the three general types of computer languages?

a. Machine languages. b. Assembly languages. c. High-Level languages. d. Spoken languages.

Computer Science & Information Technology

How many elements can be stored in a hash table T with a load factor of 40 and 10 slots?

a. 400 b. 40 c. 10 d. 4000

Computer Science & Information Technology

Bluetooth technology uses ________ waves to transmit data signals over short distances

Fill in the blank(s) with correct word

Computer Science & Information Technology

Sometimes the values stored in arrays should be constants.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology