CSE 2320 Section 060/061 Spring 1999

Program 1

Due: February 11, 1999, 1pm (February 12, 1999, 5:00pm for -10%)

Implement the recursive, divide-and-conquer algorithm BINARY-SEARCH from Homework 1, problem 5. You may write your code in C or C++, but cannot use global variables. In addition to correctness, programming style is also important, so please write modular, well-documented code.

1.
Implement the recursive, divide-and-conquer function BINARY-SEARCH(A,v,p,r) that returns the index i ( ) such that A[i] = v, or returns -1 if v does not occur in A[p..r]. You may assume there is no more than one occurrence of v in A.

2.
Implement a procedure to sort the array prior to calling BINARY-SEARCH.

3.
The main part of your program will read in data from a file whose name is given as the first argument to your executable. The first line of the input file contains the number of lines (1-99) in the rest of the file. Each subsequent line will generate a call to BINARY-SEARCH. The line begins with the value v to be searched for, followed by the number of elements n (1-99) in the array, followed by the n elements (1-99). Your main program should process each line one at a time by reading in the information, sorting the array, calling BINARY-SEARCH, and then printing the results. The result consists of the sorted array, the value to be searched for, and the result of the search. For example, if your compiled program is in a.out, and you want to run it on the sample input file pgm1in that is in the directory /public/cse/2320-061 on omega, you should do the following:

omega> a.out /public/cse/2320-061/pgm1in
A = <1 2 3 4 5 6 7 8 9>, v = 2, i = 2
A = <7 9 15 22 34>, v = 15, i = 3
A = <1 3 5 7 9 11 15 21>, v = 8, i = -1
A = <1>, v = 1, i = 1
A = <1 2>, v = 3, i = -1


Your output should match the above format as close as possible. Your program must handle every possible input, not just those in pgm1in.

4.