1 Chapter – 12 Table Lookups Table Codes –Expanded Values –Types of Codes Numeric Alphabetic Alphanumeric
2 Figure 12.1 Table of Major Codes 02ART HISTORY 04BIOLOGY 19CHEMESTRY 21CIVIL ENGINEERING 24COMP INF SYS 32ECONOMICS 39FINANCE 43MANAGEMENT 49MARKETING 54STATISTICS
3 TABLE 12.1 Types of Table Codes SYMBOLSNUMBER OF POSSIBLE VALUES CODE TYPEUSED 1 POSITION2 POSITIONS n POSITIONS Numeric = = n AlphabeticA - Z26 1 = = n AlphanumericA - Z, = = 1, n Characteristics of Codes Precise - each code is unique allowing only one alternative Expandable - allows for future growth Mnemonic - easy to remember meaning of the code
4 Characteristics of codes –Percise –Mnemonic –Expandable Sequential table lookup –N/2 attempts –Sequential check of table entries –Organization by frequency of occurrence 80/20 rule
5 Figure 12.2Sequential Table Lookup 02ART HISTORY 04BIOLOGY 19CHEMESTRY 21CIVIL ENGINEERING 24COMP INF SYS 32ECONOMICS 39FINANCE 43MANAGEMENT 49MARKETING 53STATISTICS 39 1st try 2nd try 3rd try 4th try 5th try 6th try 7th try FINANCE Match
6 Binary table lookup –Methodology –Speed advantages Positional Organization –Positional table Wasted space Address is the code Consecutive numeric codes Direct access to table elements
7 Figure 12.3Binary Lookup 02ART HISTORY 04BIOLOGY 19CHEMESTRY 21CIVIL ENGINEERING 24COMP INF SYS 32ECONOMICS 39FINANCE 43MANAGEMENT 49MARKETING 53STATISTICS 39 1st try FINANCE Match 2nd try 3rd try
8 TABLE 12.2 Required Number of Comparisons for Binary Search NUMBER OF ELEMENTSMAXIMUM NUMBER OF COMPARISONS (less than 2 4 ) (less than 2 5 ) (less than 2 6 ) (less than 2 7 ) (less than 2 8 ) (less than 2 9 ) (less than 2 10 ) (less than 2 11 ) (less than 2 12 )
9 Figure 12.4Positional Organization & Direct Lookup 02ART HISTORY 04BIOLOGY 19CHEMESTRY 21CIVIL ENGINEERING 24COMP INF SYS 32ECONOMICS 39FINANCE 43MANAGEMENT 39 1st try FINANCE Match
10 Initializing a table –Hard coding VALUE, REDEFINES, OCCURS –reading from a file Dynamically defining the size of the record –Table Lookups Perform Varying Search Statement
11 01 MAJOR-VALUE. 05 FILLERPIC X(10)VALUE ‘02ART HISTORY’. 05 FILLERPIC X(10)VALUE ‘04BIOLOGY’. 05 FILLERPIC X(10)VALUE ‘19CHEMISTRY’. 05 FILLERPIC X(10)VALUE ‘21CIVIL ENG’. 05 FILLERPIC X(10)VALUE ‘24COMP INF SYS’. 05 FILLERPIC X(10)VALUE ‘32ECONOMICS’. 05 FILLERPIC X(10)VALUE ‘39FINANCE’. 05 FILLERPIC X(10)VALUE ‘43MANAGEMENT’. 05 FILLERPIC X(10)VALUE ‘40MARKETING’. 05 FILLERPIC X(10) VALUE ‘54STATISTICS’. 01 MAJOR-TABLE REDEFINES MAJOR-VALUE. 05 MAJORS OCCURS 10 TIMES. 10 MAJOR-CODEPIC 9(2). 10 EXPANDED-MAJORPIC X(12). Figure 12.5Initialization via Hard Coding
12 Figure 12.6Table Initialization (Storage Schematic) 02ARTHISTORY 4BIOLOGY 054STATISTI MAJOR-VALUE MAJOR-TABLE... CD (1) EXP-MAJOR (1) CD (2) EXP-MAJOR (2)EXP-MAJOR (10) CD (10)
13 Figure 12.7Input-Loaded Table FD MAJOR-CODE-FILE. RECORD CONTAINS 14 CHARACTERS DATA RECORD IS MAJOR-CODE-RECORD. 01 MAJOR-CODE-RECORD. 05 INCOMING-FILE-CODEPIC 9(2). 05 INCOMING-FILE-NAMEPIC X(12).... WORKING-STORAGE SECTION. 01 MAJOR-TABLE. 05 MAJORS OCCURS 1 TO 10 TIMES DEPENDING ON NUMBER-OF-MAJORS INDEXED BY MAJOR-INDEX. 10 MAJOR-CODEPIC 9(2). 10 EXPANDED-MAJORPIC X(12). 01 NUMBER-OF-MAJORSPIC 99 VALUE ZERO. 01 END-OF-MAJOR-FILE-SWPIC XVALUE SPACES. 88 END-OF-MAJOR-FILEVALUE ‘Y’. (a) Data Division entries
14 Figure 12.7Input-Loaded Table PROCEDURE DIVISION. OPEN INPUT MAJOR-CODE-FILE. PERFORM VARYING MAJOR-INDEX FROM 1 BY 1 UNTIL MAJOR-INDEX > 10 OR END-OF-MAJOR-FILE READ MAJOR-CODE-FILE AT END MOVE ‘Y’ TO END-OF-MAJOR-FILE NOT AT END ADD 1 TO NUMBER-OF-MAJORS MOVE INCOMING-FILE-CODE TO MAJOR-CODE (MAJOR-INDEX) MOVE INCOMING-FILE-NAME TO EXPANDED-MAJOR (MAJOR-INDEX) END-READ END-PERFORM. IF MAJOR-INDEX > 10 DISPLAY ‘MAJOR TABLE TOO SMALL’ END-IF. CLOSE MAJOR-CODE-FILE PERFORM 0100-PREPARE-STUDENT-TRANSCRIPT.... (b) In-Line Perform
15 How many unique codes can be developed from a 4 position numeric code, alphabetic code, 4 position alphanumeric code.
16 What’s wrong with this code? 01 MONTH-TABLE. 05 MONTH OCCURS 12 TIMES PIC X(4). 05 MONTH-VALUES REDEFINES MONTH PIC X(36) VALUE ‘JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC’.
17 Table Lookups –Using the PERFORM.. VARYING statement –Using the SEARCH statement Must use indexes –Using the SEARCH ALL statement Uses index plus codes must be in order Uses a binary search Does not use a set command –Direct lookup Positional table –Range-step tables No one-to-one correspondence
18 19CHEMISTRY Figure 12.8Input-Loaded Tables 04BIOLOGY 02ART HISTORY 02 ART HISTORY04BIOLOGY19CHEMISTRY MAJOR-CODE (1) EXPANDED-MAJOR (1) MAJOR-CODE (2) EXPANDED-MAJOR (2) MAJOR-CODE (3) EXPANDED-MAJOR (3)
19 Figure 12.9Sequential Lookup with PERFORM VARYING WORKING-STORAGE SECTION. 01 TABLE-PROCESSING-ELEMENTS. 05 WS-MAJOR-SUBPIC S9(4)COMP. 05 WS-FOUND-MAJOR-SWITCHPIC X(3)VALUE ‘NO’. 05 WS-END-OF-TABLE-SWITCHPIC X(3)VALUE ‘NO’. 01 MAJOR-VALUE. 05 FILLERPIC X(10)VALUE ‘02ART HISTORY’. 05 FILLERPIC X(10)VALUE ‘04BIOLOGY’. 05 FILLERPIC X(10)VALUE ‘19CHEMISTRY’. 05 FILLERPIC X(10)VALUE ‘21CIVIL ENG’. 05 FILLERPIC X(10)VALUE ‘24COMP INF SYS’. 05 FILLERPIC X(10)VALUE ‘32ECONOMICS’. 05 FILLERPIC X(10)VALUE ‘39FINANCE’. 05 FILLERPIC X(10)VALUE ‘43MANAGEMENT’. 05 FILLERPIC X(10)VALUE ‘40MARKETING’. 05 FILLERPIC X(10) VALUE ‘54STATISTICS’. 01 MAJOR-TABLE REDEFINES MAJOR-VALUE. 05 MAJORS OCCURS 10 TIMES. 10 MAJOR-CODEPIC 9(2). 10 EXPANDED-MAJORPIC X(12).
20 Figure 12.9Sequential Lookup with PERFORM VARYING PROCEDURE DIVISION PREPARE-STUDENT-REPORT. MOVE ‘NO’ TO WS-FOUND-SWITCH WS-END-OF-TABLE-SWITCH. PERFORM 1000-FIND-MAJOR VARYING WS-MAJOR-SUB FROM 1 BY 1 UNTIL WS-END-OF-TABLE-SWITCH = ‘YES’ OR WS-FOUND-MAJOR-SWITCH = ‘YES’ FIND-MAJOR. IF WS-MAJOR-SUB > 10 MOVE ‘YES’ TO WS-END-OF-TABLE-SWITCH MOVE ‘UNKNOWN’ TO HDG-MAJOR ELSE IF ST-MAJ0R-CODE = MAJOR-CODE (WS-MAJOR-SUB) MOVE ‘YES’ TO WS-FOUND-MAJOR-SWITCH MOVE EXP-MAJOR (WS-MAJOR-SUB) TO HDG-MAJOR END-IF END-IF.
21 Figure 12.10SEARCH Statement (Sequential Lookup) 01 MAJOR-VALUE. 05 FILLERPIC X(10)VALUE ‘02ART HISTORY’. 05 FILLERPIC X(10)VALUE ‘04BIOLOGY’. 05 FILLERPIC X(10)VALUE ‘19CHEMISTRY’. 05 FILLERPIC X(10)VALUE ‘21CIVIL ENG’. 05 FILLERPIC X(10)VALUE ‘24COMP INF SYS’. 05 FILLERPIC X(10)VALUE ‘32ECONOMICS’. 05 FILLERPIC X(10)VALUE ‘39FINANCE’. 05 FILLERPIC X(10)VALUE ‘43MANAGEMENT’. 05 FILLERPIC X(10)VALUE ‘40MARKETING’. 05 FILLERPIC X(10) VALUE ‘54STATISTICS’. 01 MAJOR-TABLE REDEFINES MAJOR-VALUE. 05 MAJORS OCCURS 10 TIMES INDEXED BY MAJOR-INDEX. 10 MAJOR-CODEPIC 9(2). 10 EXPANDED-MAJORPIC X(12).... INDEXED BY clause required in table definition
22 Figure 12.10SEARCH Statement (Sequential Lookup) PROCEDURE DIVISION.... SET MAJOR-INDEX TO 1. SEARCH MAJORS AT END MOVE ‘UNKNOWN’ TO HDG-MAJOR WHEN ST-MAJOR-CODE = MAJOR-CODE (MAJOR-INDEX) MOVE EXP-MAJOR (MAJOR-INDEX) TO HDG-MAJOR END-SEARCH. SET statement establishes starting point
23 Figure 12.11SEARCH ALL Statement (Binary Lookup) KEY required for binary search (ASCENDING or DESCENDING) 01 MAJOR-VALUE. 05 FILLERPIC X(10)VALUE ‘02ART HISTORY’. 05 FILLERPIC X(10)VALUE ‘04BIOLOGY’. 05 FILLERPIC X(10)VALUE ‘19CHEMISTRY’. 05 FILLERPIC X(10)VALUE ‘21CIVIL ENG’. 05 FILLERPIC X(10)VALUE ‘24COMP INF SYS’. 05 FILLERPIC X(10)VALUE ‘32ECONOMICS’. 05 FILLERPIC X(10)VALUE ‘39FINANCE’. 05 FILLERPIC X(10)VALUE ‘43MANAGEMENT’. 05 FILLERPIC X(10)VALUE ‘40MARKETING’. 05 FILLERPIC X(10) VALUE ‘54STATISTICS’. 01 MAJOR-TABLE REDEFINES MAJOR-VALUE. 05 MAJORS OCCURS 10 TIMES ASCENDING KEY IS MAJOR-CODE INDEXED BY MAJOR-INDEX. 10 MAJOR-CODEPIC 9(2). 10 EXPANDED-MAJORPIC X(12)....
24 Figure 12.11SEARCH ALL Statement (Binary Lookup) PROCEDURE DIVISION.... SEARCH ALL MAJORS AT END MOVE ‘UNKNOWN’ TO HDG-MAJOR WHEN MAJOR-CODE (MAJOR-INDEX) = ST-MAJOR-CODE MOVE EXP-MAJOR (MAJOR-INDEX) TO HDG-MAJOR END-SEARCH.
25 Figure Direct Access to Table Entries 01 MAJOR-VALUE. 05 FILLERPIC X(12) VALUE ‘UNKNOWN’. 05 FILLERPIC X(12) VALUE ‘ACCOUNTING’. 05 FILLERPIC X(12) VALUE ‘UNKNOWN’. 05 FILLERPIC X(12) VALUE ‘BIOLOGY’ FILLERPIC X(12) VALUE ‘STATISTICS’. 01 MAJOR-TABLE REDEFINES MAJOR-VALUE. 05 MAJORS OCCURS 54 TIMES PIC X(12).... PROCEDURE DIVISION.... IF ST-MAJOR-CODE > 0 AND ST-MAJOR-CODE < 55 MOVE MAJORS (ST-MAJOR-CODE) TO HDG-MAJOR ELSE MOVE ‘UNKNOWN’ TO HDG-MAJOR END-IF. Positional organization results in wasted space Check to ensure valid code
26 Figure Range-step Table Grade PointScholarship Average Percentage (a) Scholarship Table
27 Figure Range-step Table 01 SCHOLARSHIP-TABLE. 05 GPA-SCHOLARSHIP-PERCENTAGES. 10 FILLERPIC X(6) VALUE ‘375100’. 10 FILLERPIC X(6) VALUE ‘350075’. 10 FILLERPIC X(6) VALUE ‘325050’. 10 FILLERPIC X(6) VALUE ‘300033’. 10 FILLERPIC X(6) VALUE ‘275025’. 10 FILLERPIC X(6) VALUE ‘250015’. 05 GPA-TABLE REDEFINES GPA-SCHOLARSHIP-PERCENTAGES OCCURS 6 TIMES INDEXED BY GPA-INDEX. 10 GPA-MINIMUM PIC 9V SCHOLARSHIP-PCT PIC SET GPA-INDEX TO 1. SEARCH GPA-TABLE AT END MOVE ZERO TO SCHOLARSHIP-AWARD WHEN STUDENT-GPA >= GPA-MINIMUM (GPA-INDEX) MOVE SCHOLARSHIP-PCT (GPA-INDEX) TO SCHOLARSHIP-AWARD END-SEARCH. (b) COBOL Implementation
28 Summary Codes & types VALUE, OCCURS, & REDEFINES used to define and initialize a table. Table lookups – sequential, binary, direct access Range-step table Tables are initialized by hard-coding or dynamically loading at execution time SEARCH statement, requires an index SEARCH ALL statement, index & in order