CS202 Computer Science II
FAQ Newsboard  Telnet Email TA Lectures Assignments Man pages Help

Assignment 4
Due Oct 19th before 9:00 a.m. in class


Last modified 10/11/98 @ 5:00 p.m.
A Makefile, and Turnin intructions

This assignment will get you more familiar with recursion.

Write a recursive version of the binary search algorithm that takes an integer key value and a sorted array of integers as its input and searches for the key in the array.

Your program should read a sorted list of integers from standard input, search for the key (specified on the command line), and output the index of the key in the list, if found, and an appropriate message if the key is not found. Both the number of integers to be read and the key are specified on the command line.

	% bsearch 10 25
would read 10 integers from standard input into an array and search for the value 25. In general, bsearch will be invoked as follows:
	% bsearch n key
where

Your output should also indicate the number of comparisons made. Here are some examples with my inputfile file named Intinput.

% bsearch 23 162 < Intinput
Found 162 in location  9 after 4 Comparisons  
% bsearch 23 629 < Intinput
Found 629 in location  22 after 4 Comparisons   
% bsearch 24 629 < Intinput  
Found 629 in location  22 after 5 Comparisons  
% bsearch 24 19 < Intinput 
Could not find 19 in array after 4 Comparisons  
Here is my main source file (as4.cpp) provided as a template.

Constraints

  1. ReadInput() is expected to be in a file named io.cpp and
  2. BSearch() to be in the file bsearch.cpp

Good Luck. Start on the assignment now.

Please follow these instructions for turning in assignments.