next up previous

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.

Implement the recursive, divide-and-conquer function BINARY-SEARCH(A,v,p,r) that returns the index i ( $p \leq i
\leq r$) 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.

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

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.

Be sure to properly document your source code. If you are unsure about the level of comments, please ask the GTA, who will be grading your programs. Also, in the comment at the top of your source code, you must include the homework number, your full name, ID number, login, class (2320), and a line indicating how to compile your program (e.g., compile using: cc or compile using: gcc -lm).

After ensuring that your program compiles and executes correctly on the omega machine, run the program /public/cse/2320-061/handin1 to electronically hand in your source file. All source code must be in one file (i.e., no separate .h and .c files). The handin program will prompt you for the file name containing your source code and will output the date and time when your program was handed in. Be sure you are in the directory containing your source file when you run the handin program. Note: You may run the handin program as many times as you like, but only the most recently handed in file will be retained. Do not turn in a printout of your source code in class.

next up previous