Presentation is loading. Please wait.

Presentation is loading. Please wait.

Searching an Array or Table

Similar presentations


Presentation on theme: "Searching an Array or Table"— Presentation transcript:

1 Searching an Array or Table
Please use speaker notes for additional information! This presentation deals with the concept of indirect subscripts/indexes/pointers and searching an array or table.

2 This shows the input data, the array/table that will be searched and the names that will be retrieved from the table. Item Number on input records 111112 121227 122445 122528 200317 202728 300303 301034 401215 407850 500019 501224 CORN CHOWDER ONION SOUP WONTON SOUP GREEN PEA SOUP TOMATO SOUP SEAFOOD CHOWDER *** INVALID *** CLAM CHOWDER CHICKEN SOUP 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. Note that I have drawn arrows for only some of the combinations. It got crowded! The colors show the relationship between the input and the output!

3 Note that I am going to refer to the data as:
itemNoArray which stands for the item number in the array/table itemNameArray which stands for the item name in the array/table itemNoIn which is the item number on the input that I am going to compare to itemNoArray to find a match itemNoArray itemNameArray Input Data orderNoIn itemNoIn 111112 121227 122445 122528 200317 202728 300303 301034 401215 407850 500019 501224 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. The valid item numbers have been set up in the array/table with a related item name. The valid numbers are: 03, 12, 15, 17, 24, 25, 27,28, 45. Data file

4 In this code, I am controlling the search:
Control of the search - step #1 SET itemSub to 1 SET matchInd to “NO” LOOP UNTIL itemSub > 9 OR matchInd = "YES" IF itemNoIn = itemNoTable(itemSub) Set matchInd to “YES” ELSE Increment itemSub by 1 ENDIF END LOOP IF matchInd = "YES" MOVE itemNameTable (itemSub) TO output MOVE "*** INVALID ***" TO output itemSUB matchInd NO In this code, I am controlling the search: STEP #1: I initialize the index/pointer/subscript, itemSub to 1 and the matchInd to NO. By setting the itemSub to 1, I am saying that I want to start with the first element in the array. By sent the matchInd to NO I am saying I have not yet found a match. When I do find a match, I will change the matchInd to YES. 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.

5 Control of the search - step #2
First record: itemNoIn = 12 SET itemSub to 1 SET matchInd to “NO” LOOP UNTIL itemSub > 9 OR matchInd = "YES" IF itemNoIn = itemNoTable(itemSub) Set matchInd to “YES” ELSE Increment itemSub by 1 ENDIF END LOOP IF matchInd = "YES" MOVE itemNameTable (itemSub) TO output MOVE "*** INVALID ***" TO output itemSUB matchInd NO The loop is done until either itemSub is greater than 9 meaning I have checked everything (there are 9 elements in the table) or I find a match. On the first IF, I test the record on the input which is 12 with the element in the array/table that itemSub is pointing to. Since itemSub is a 1, I am comparing 12 and 3. They are not equal, so I drop down to the ELSE and increment itemSub by 1. 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. itemSUB matchInd NO 2

6 Since itemSub is not > 9 and matchInd = “NO”, I reenter the loop.
Control of the search - step #3 First record: itemNoIn = 12 SET itemSub to 1 SET matchInd to “NO” LOOP UNTIL itemSub > 9 OR matchInd = "YES" IF itemNoIn = itemNoTable(itemSub) Set matchInd to “YES” ELSE Increment itemSub by 1 ENDIF END LOOP IF matchInd = "YES" MOVE itemNameTable (itemSub) TO output MOVE "*** INVALID ***" TO output itemSUB matchInd NO Since itemSub is not > 9 and matchInd = “NO”, I reenter the loop. On the second IF, I test the record on the input which is 12 with the element in the array/table that itemSub is pointing to. Since itemSub is a 2, I am comparing 12 and 12. They are equal, so I set match-Ind to “YES”. This means on the next check, I will not reenter the loop 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. itemSUB matchInd NO 2 YES

7 Since matchInd is YES, I do not reenter the loop.
Control of the search - step #4 First record: itemNoIn = 12 SET itemSub to 1 SET matchInd to “NO” LOOP UNTIL itemSub > 9 OR matchInd = "YES" IF itemNoIn = itemNoTable(itemSub) Set matchInd to “YES” ELSE Increment itemSub by 1 ENDIF END LOOP IF matchInd = "YES" MOVE itemNameTable (itemSub) TO output MOVE "*** INVALID ***" TO output itemSUB matchInd YES Since matchInd is YES, I do not reenter the loop. I now execute the if statement below the loop. Since matchInd = YES I move itemNameTable (itemSub) which means itemNameTable(2) to output. CLAM CHOWDER IS MOVED TO OUTPUT. 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. CORN CHOWDER

8 THERE ARE THREE LOGICAL COMPONENTS TO THE SEARCH:
SET itemSub to 1 SET matchInd to “NO” LOOP UNTIL itemSub > 9 OR matchInd = "YES" IF itemNoIn = itemNoTable(itemSub) Set matchInd to “YES” ELSE Increment itemSub by 1 ENDIF END LOOP IF matchInd = "YES" MOVE itemNameTable (itemSub) TO output MOVE "*** INVALID ***" TO output Logic of the search THERE ARE THREE LOGICAL COMPONENTS TO THE SEARCH: 1) INITIALIZING FOR THE SEARCH - setting the two things that will control the search to their initial values. Note that both things are acted on within the search itself so they have the potential to change. 2) PERFORMING THE SEARCH until one of the two things that we initialized indicate that the search is done. itemSub >9 means the search was unsuccessful and that all of the items have been checked. matchInd = YES means the search was successful (a match was found). The search itself simple compares the input to the item in the table that itemSub is pointing to. If they match the matchInd is set to YES, otherwise itemSub is incremented by 1. 3) DETERMINE PROCESSING BASED ON WHY THE SEARCH ENDED - If matchInd = YES, then a match was found and the name with the matching number is moved to the line. Otherwise the search ended because itemSub > 9 which means the search was unsuccessful and an error message is moved to the line. 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.

9 LOOP UNTIL LOOP WHILE SET itemSub to 1 SET matchInd to “NO” LOOP
UNTIL itemSub > 9 OR matchInd = "YES" IF itemNoIn = itemNoTable(itemSub) Set matchInd to “YES” ELSE Increment itemSub by 1 ENDIF END LOOP IF matchInd = "YES" MOVE itemNameTable (itemSub) TO output MOVE "*** INVALID ***" TO output LOOP UNTIL SET itemSub to 1 SET matchInd to “NO” LOOP WHILE itemSub < 10 AND matchInd = ”NO" IF itemNoIn = itemNoTable(itemSub) Set matchInd to “YES” ELSE Increment itemSub by 1 ENDIF END LOOP IF matchInd = "YES" MOVE itemNameTable (itemSub) TO output MOVE "*** INVALID ***" TO output LOOP WHILE 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.

10 Search Set sub to 1 and matchInd to NO Sub < 10 AND matchInd = NO Y
Input = element in array(sub) N Y Increment sub by 1 matchInd to Yes N 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. matchInd = YES N Y Error message to output Name element from array (sub) to output End Search

11 Search Set sub to 1 and matchInd to NO Sub < 10 AND matchInd = NO Y
Loop N matchInd = YES Y N Search Loop Error message to output Name element from array (sub) to output Input = element in array(sub) Y N 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. Increment sub by 1 matchInd to Yes End Search End Search Loop

12 IF itemNoIn = itemNoTable(itemSub) Set matchInd to “YES” ELSE
search SET itemSub to 1 SET matchInd to “NO” LOOP UNTIL itemSub > 9 OR matchInd = "YES” Do the SEARCH LOOP END LOOP IF matchInd = "YES" MOVE itemNameTable (itemSub) TO output ELSE MOVE "*** INVALID ***" TO output ENDIF END SEARCH SEARCH SET itemSub to 1 SET matchInd to “NO” LOOP WHILE itemSub < 10 AND matchInd = ”NO” Do the SEARCH LOOP END LOOP IF matchInd = "YES" MOVE itemNameTable (itemSub) TO output ELSE MOVE "*** INVALID ***" TO output ENDIF END SEARCH Search Loop IF itemNoIn = itemNoTable(itemSub) Set matchInd to “YES” ELSE Increment itemSub by 1 ENDIF End Search Loop 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.

13 Set up variables and array
Mainline Housekeeping Process Read a record Housekeeping Set up variables and array Process Open files Not EOF Process Record Loop Y N End Housekeeping Wrapup End Process End Mainline Wrapup 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. Close Files End Wrapup

14 A A Process record loop Move data from record to output Write output
Read a record Set sub to 1 and matchInd to NO Sub < 10 AND matchInd = NO End Process Record Loop Y Search Loop N matchInd = YES N Y 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. Error message to output Name element from array (sub) to output A

15 Search Loop Input = element in array(sub) N Y Increment sub by 1
matchInd to Yes End Search Loop This complete the logic flowchart for a program that must search an array/table in the process of creating output.


Download ppt "Searching an Array or Table"

Similar presentations


Ads by Google