Inlab 25

Problem 1:

Write a program with the following requirements:

1.  Write a seqential search function to search searchitem in the array list, the length of array is listLength, if searchitem is found, return the position of the element, if searchitem is not found, return -1.

The following is the function prototype:

int seqSearch(const int list[], int listLength, int searchItem);

2.  In the main function:

l  Declare an integer-type array of size 10.

l  prompt user to input 10 integers and using for loop to save them in the array.

l  Prompt user to input an integer to be searched and save this integer to the variable with the name of number;

l  Call function seqSearch to search if number is in the array.

l  If number is found in the array, print out “search success” and print out the position of the element

l  If number is not found in the array, print out “search fails”

Problem 2:

Change the above program to search string in the string arrays:

·  Suppose that you have an array that holds strings declared as follows:

string list[10];

·  The above function prototype is changed to:

void seqSearch(string list[], int listLength, string searchItem);

This function will use the linear(sequential) search algorithm to search searchItem in the array list and print out “YES” if the word “hello” is in the array, and print out “NO” otherwise.

Problem 3 Extra Credit:

Write a program with the following requirements:

1.  Write a fuction to Input the array in the ascending order.

The following is the function prototype:

void InputArray(int list[], int length);

2.  Write a binary search function to search searchitem in the array list, the length of array is listLength, if searchitem is found, return the position of the element, if searchitem is not found, return -1.

The following is the function prototype:

int bianrySearch(const int list[], int listLength, int searchItem);

3.  In the main function:

l  Declare an integer-type array of size 10.

l  Call function InputArray to input the elements in array.

l  Prompt user to input an integer to be searched and save this integer to the variable with the name of number;

l  Call function binarySearch to search if number is in the array.

l  If number is found in the array, print out “search success” and print out the position of the element

l  If number is not found in the array, print out “search fails”.