FAQ | Newsboard | Telnet | Email TA | Lectures | Assignments | Man pages | Help |
---|
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 25would 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 keywhere
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 ComparisonsHere is my main source file (as4.cpp) provided as a template.
Constraints
Good Luck. Start on the assignment now.
Please follow these instructions for turning in assignments.