Binary Search Assignment:

After reading the separate notes on a binary search and the notes below, your assignment is to write the binary search in COBOL. If you pass in a coded search, the maximum grade you can earn is a B. If you run the program, the maximum grade is an A+. NOTE: THIS ASSIGNMENT MUST BE PASSED IN WITHIN A WEEK FROM THE DATE POSTED BECAUSE I WANT TO DISCUSS THE SOLUTION. Also please note that the SEARCH verb is our next topic - it CANNOT be used for this assignment. This is an assignment to figure out the logic!


There are two kinds of searches that we are discussing: linear and binary. A linear search is the kind of search we have been looking at this semester and during CIS12. With a linear search you can have a direct subscript if the numbers in the code are sequential starting at 1 with few gaps or an indirect subscript if the numbers are widely varied or if the code is non numeric. Yes, you could have codes such as BU, CI, and OF and search for a match just as you do with numeric codes!

A binary search is applicable for large amounts of data and data where the codes in the table are in order. Read the other notes on a binary search for a complete explanation.

Comparing the logic for a binary search and a linear search:

Linear Search

Binary search

Initializes for a search by moving 1 to subscript and setting a match-ind.

Initializes for search establishing upper and lower limit and calculating the subscript and setting a match-ind.

Performs the search loop until a match is found or the subscript is out of range indicating everything has been checked (can also include an early exit).

Performs the search loop until a match is found of the lower limit equals the subscript indicating everything has been checked. (You might also do this by having the indicator change to YES for a found and NO for a lower limit = subscript.)

Inside the search compares the number coming in to be checked against the number in the table that the subscript is pointing to.

Inside the search compares the number coming in to be checked against the number in the table that the subscript is pointing to.

Increments the subscript by adding 1 if not found.

Changes the subscript by doing the upper + lower divided by 2 calculation.

After search loop has been exited determines whether match found or error.

After search loop has been exited determines whether match found or error.

To test this program, I recommend using the DISPLAY and ACCEPT. Briefly, COBOL allows interaction on the screen in two ways. Full screen processing is done with the SCREEN SECTION. Line by line processing on the screen can be done with just the DISPLAY and ACCEPT. Display puts out information on the screen and ACCEPT takes in information. DISPLAY does not pause processing, ACCEPT does pause processing while it waits for a user response. Therefore the ACCEPT has the added value of giving you time to read the screen. DISPLAY and ACCEPT do not need entries in the environment division and are easy tools to use for testing a program.

Essentially the combination of DISPLAY and ACCEPT can be used in place of a READ in the logic when you want user input instead of input from a file. An example would be:

DISPLAY 'Enter number to be found '.


NUM-IN would be defined in WORKING-STORAGE and would be big enough to hold the largest number inputted by the user.

When you find a match instead of moving to a print line, you can simply display on the screen. In this case you can DISPLAY and just have it go to the screen unseen by you (to be viewed when the program is done) or you can again do a DISPLAY/ACCEPT combination using the ACCEPT to stop processing long enough to see the screen. In that case, I define a variable called JUNK or some similar name in WS and ACCEPT JUNK.

DISPLAY 'The answer is " NAM-T(SUBZ).


In this case I am displaying The answer is followed by a space (note that the space is inside the quotes) and the NAM-T from the table modified by the subscript - I am assuming that this was the result of the search. Then the ACCEPT will pause until a key is pressed. Whatever is pressed goes to JUNK but we will never test that because the ACCEPT was just there to pause the program.

As a side note: DISPLAY and ACCEPT are excellent debugging tools. If a subscript doesn't seem to be working correctly or a total doesn't seem to be calculating correctly etc., the programmer can DISPLAY a message telling where you are in the program and display the content of the field that is causing trouble. The DISPLAY/ACCEPT can be put in the key places in the code where things could be going wrong so that the programmer can see the results as they process. Obviously this can be accomplished by stepping through the program as well, but using the DISPLAY/ACCEPT allows you to easily focus on key locations.