Separate speaker notes to accompany arrary search using an indirect subscript (arraysearch):
This presentation deals with the concept of indirect subscripts/indexes/pointers and searching an array or table.
The input record contains an item number that is not in the sequence 1, 2, 3. Therefore the input item number can not be used as a direct pointer/subscript/index for the array/ table. Instead, the programmer must develop a search to find the matching item number in the array/ table and then use an indirect pointer or subscript to get the name out of the table.
This shows a sample data file that might be read and a sample table/array. The valid numbers are numbers that appear in the table/array. Other numbers do not have an item name associated with them so they are not valid.
I am going to show the steps in the search in the next few slides. Step 1 is initializing prior to the search. Whenever I enter the search loop, I want to make sure that the subscript/index/pointer is starting at 1 and the flag or indicator I am using to tell me when I found a match is starting at NO.
This shows entering the loop and then executing the if to test the input against the item in the array/table that itemSub is pointing to. Since itemSub was a 1 I am comparing the input with the first element in the array/table.
The value in itemSub is a 2. When I compare 12 to the second element in the table, it is a match. This causes me to set matchInd to YES.
Since the loop was done only until itemSub was greater than 9 or matchInd = YES, I will now exit the loop based on matchInd being YES.
After exiting the loop, I test matchInd. The reason is that I have two reasons for exiting the loop. The first reason is that itemSub is > 9 and the second reason is that matchInd = YES. I have to find out which reason caused me to exit.
When I test, I find that I left because matchInd = YES, so I move the name of the second element from the array/table to the output. To get the second element, I use itemSub which is set to 2 because that is where it found the match.
This shows the basic logic of the search.
Note the basic structure is like so many loops we have looked at.
Initialize before entering
Loop until criteria are met
Have code with in the loop that effect the criteria being tested so that we are sure we will eventually exit.
Note that this could have been coded with a while instead of an until. Note the difference in the test to determine if the loop is to be executed again or exited.
Since I am testing against 10 in the WHILE example, I need to set the memory variable that holds the number big enough to hold two characters.
This shows the if statement embedded in the loop the way I showed it in pseudocode. You could also have the if statement in a separate routine. See the next slide.
This shows the search loop as a separate procedure which a lot of people find easier to understand and easier to code. The code that accompanies this is on the next slide.
This shows the code to put the actually comparison in a subroutine that is executed over and over from the loop until either the item subscript is no longer less than 10 or the match ind = YES.
The code is written with the loop while structure and the loop until structure.
This slide will put the search into the context of a program. I am doing the standard housekeeping, processing and wrapup. In the processing I will ouput information from the record including the name of the item that I get by doing a search.
This shows the loop that I use to set up output write and read another record. Incorporated into the setup of the output is the search.
See next slide for search loop.
This completes the logic flowchart for a program that must search an array/table in the process of creating output.