Chapter 12 Table Lookups Table Codes Expanded Values

Chapter – 12 Table Lookups • Table Codes –Expanded Values –Types of Codes • Numeric • Alphabetic • Alphanumeric 1

Figure 12. 1 Table of Major Codes 02 04 19 21 24 32 39 43 49 54 ART HISTORY BIOLOGY CHEMESTRY CIVIL ENGINEERING COMP INF SYS ECONOMICS FINANCE MANAGEMENT MARKETING STATISTICS 2

TABLE 12. 1 SYMBOLS Types of Table Codes NUMBER OF POSSIBLE VALUES CODE TYPE USED 1 POSITION 2 POSITIONS n POSITIONS Numeric 0 -9 101 = 10 102 = 100 10 n Alphabetic A-Z 261 = 26 262 = 676 26 n Alphanumeric A - Z, 0 - 9 361 = 36 362 = 1, 296 36 n Characteristics of Codes • Precise - each code is unique allowing only one alternative • Mnemonic - easy to remember meaning of the code • Expandable - allows for future growth 3

• 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 4

Figure 12. 2 Sequential Table Lookup 39 1 st try 2 nd try 3 rd try 4 th try 5 th try 6 th try 7 th try 02 ART HISTORY 04 BIOLOGY 19 CHEMESTRY 21 CIVIL ENGINEERING 24 COMP INF SYS 32 ECONOMICS 39 FINANCE Match 43 MANAGEMENT 49 MARKETING 53 STATISTICS 5

• Binary table lookup – Methodology – Speed advantages • Positional Organization – Positional table • • Wasted space Address is the code Consecutive numeric codes Direct access to table elements 6

Figure 12. 3 Binary Lookup 39 02 ART HISTORY 04 BIOLOGY 19 CHEMESTRY 1 st try 21 CIVIL ENGINEERING 24 COMP INF SYS 3 rd try 2 nd try 32 ECONOMICS 39 FINANCE Match 43 MANAGEMENT 49 MARKETING 53 STATISTICS 7

TABLE 12. 2 Required Number of Comparisons for Binary Search NUMBER OF ELEMENTS MAXIMUM NUMBER OF COMPARISONS 8 - 15 (less than 24) 4 16 - 31 (less than 25) 5 32 - 63 (less than 26) 6 64 - 127 (less than 27) 7 128 - 255 (less than 28) 8 256 - 511 (less than 29) 9 512 - 1023 (less than 210) 10 1024 - 2047 (less than 211) 11 2048 - 4095 (less than 212) 12 8

Figure 12. 4 Positional Organization & Direct Lookup 02 ART HISTORY 04 BIOLOGY. . . 19 CHEMESTRY 39 21 CIVIL ENGINEERING 24 COMP INF SYS. . . 32 ECONOMICS 1 st try . . . 39 FINANCE Match . . . 43 MANAGEMENT. . . 9

• 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 10

Figure 12. 5 Initialization via Hard Coding 01 01 MAJOR-VALUE. 05 FILLER 05 FILLER 05 FILLER PIC PIC PIC X(10) X(10) X(10) VALUE VALUE VALUE ‘ 02 ART HISTORY’. ‘ 04 BIOLOGY’. ‘ 19 CHEMISTRY’. ‘ 21 CIVIL ENG’. ‘ 24 COMP INF SYS’. ‘ 32 ECONOMICS’. ‘ 39 FINANCE’. ‘ 43 MANAGEMENT’. ‘ 40 MARKETING’. ‘ 54 STATISTICS’. MAJOR-TABLE REDEFINES MAJOR-VALUE. 05 MAJORS OCCURS 10 TIMES. 10 MAJOR-CODE PIC 9(2). 10 EXPANDED-MAJOR PIC X(12). 11

Figure 12. 6 Table Initialization (Storage Schematic) MAJOR-VALUE 0 2 A R T H I S T O R Y CD (1) EXP-MAJOR (1) 0 4 B I O L O G Y CD (2) EXP-MAJOR (2) . . . 5 4 S T A T I S T I CD (10) EXP-MAJOR (10) MAJOR-TABLE 12

Figure 12. 7 Input-Loaded Table FD MAJOR-CODE-FILE. RECORD CONTAINS 14 CHARACTERS DATA RECORD IS MAJOR-CODE-RECORD. 05 INCOMING-FILE-CODE PIC 9(2). 05 INCOMING-FILE-NAME PIC X(12). 01. . . WORKING-STORAGE SECTION. 01 MAJOR-TABLE. 05 MAJORS OCCURS 1 TO 10 TIMES DEPENDING ON NUMBER-OF-MAJORS INDEXED BY MAJOR-INDEX. 10 MAJOR-CODE PIC 9(2). 10 EXPANDED-MAJOR PIC X(12). 01 NUMBER-OF-MAJORS PIC 99 VALUE ZERO. 01 END-OF-MAJOR-FILE-SW 88 END-OF-MAJOR-FILE PIC X VALUE SPACES. VALUE ‘Y’. (a) Data Division entries 13

Figure 12. 7 Input-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 14

• How many unique codes can be developed from a 4 position numeric code, alphabetic code, 4 position alphanumeric code. 15

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’. 16

• 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 17

Figure 12. 8 Input-Loaded Tables 19 CHEMISTRY 04 BIOLOGY 02 ART HISTORY 02 ART HISTORY EXPANDED-MAJOR (1) MAJOR-CODE (1) 04 BIOLOGY 19 EXPANDED-MAJOR (2) MAJOR-CODE (2) CHEMISTRY EXPANDED-MAJOR (3) MAJOR-CODE (3) 18

Figure 12. 9 Sequential Lookup with PERFORM VARYING WORKING-STORAGE SECTION. 01 TABLE-PROCESSING-ELEMENTS. 05 WS-MAJOR-SUB 05 WS-FOUND-MAJOR-SWITCH 05 WS-END-OF-TABLE-SWITCH 01 01 MAJOR-VALUE. 05 FILLER 05 FILLER 05 FILLER PIC S 9(4) PIC X(3) COMP. VALUE ‘NO’. PIC PIC PIC VALUE VALUE VALUE X(10) X(10) X(10) ‘ 02 ART HISTORY’. ‘ 04 BIOLOGY’. ‘ 19 CHEMISTRY’. ‘ 21 CIVIL ENG’. ‘ 24 COMP INF SYS’. ‘ 32 ECONOMICS’. ‘ 39 FINANCE’. ‘ 43 MANAGEMENT’. ‘ 40 MARKETING’. ‘ 54 STATISTICS’. MAJOR-TABLE REDEFINES MAJOR-VALUE. 05 MAJORS OCCURS 10 TIMES. 10 MAJOR-CODE PIC 9(2). 10 EXPANDED-MAJOR PIC X(12). 19

Figure 12. 9 Sequential Lookup with PERFORM VARYING PROCEDURE DIVISION. 0000 -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’. . 1000 -FIND-MAJOR. IF WS-MAJOR-SUB > 10 MOVE ‘YES’ TO WS-END-OF-TABLE-SWITCH MOVE ‘UNKNOWN’ TO HDG-MAJOR ELSE IF ST-MAJ 0 R-CODE = MAJOR-CODE (WS-MAJOR-SUB) MOVE ‘YES’ TO WS-FOUND-MAJOR-SWITCH MOVE EXP-MAJOR (WS-MAJOR-SUB) TO HDG-MAJOR END-IF. 20

Figure 12. 10 SEARCH Statement (Sequential Lookup) 01 MAJOR-VALUE. 05 FILLER 05 FILLER 05 FILLER 01 PIC PIC PIC X(10) X(10) X(10) VALUE VALUE VALUE ‘ 02 ART HISTORY’. ‘ 04 BIOLOGY’. ‘ 19 CHEMISTRY’. ‘ 21 CIVIL ENG’. ‘ 24 COMP INF SYS’. ‘ 32 ECONOMICS’. ‘ 39 FINANCE’. ‘ 43 MANAGEMENT’. ‘ 40 MARKETING’. ‘ 54 STATISTICS’. MAJOR-TABLE REDEFINES MAJOR-VALUE. INDEXED BY clause required in table 05 MAJORS OCCURS 10 TIMES definition INDEXED BY MAJOR-INDEX. 10 MAJOR-CODE PIC 9(2). 10 EXPANDED-MAJOR PIC X(12). . 21

Figure 12. 10 SEARCH Statement (Sequential Lookup) PROCEDURE DIVISION. . . SET statement establishes starting point . 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. 22

Figure 12. 11 01 MAJOR-VALUE. 05 FILLER 05 FILLER 05 FILLER 01 SEARCH ALL Statement (Binary Lookup) PIC PIC PIC X(10) X(10) X(10) VALUE VALUE VALUE ‘ 02 ART HISTORY’. ‘ 04 BIOLOGY’. ‘ 19 CHEMISTRY’. ‘ 21 CIVIL ENG’. ‘ 24 COMP INF SYS’. ‘ 32 ECONOMICS’. ‘ 39 FINANCE’. ‘ 43 MANAGEMENT’. ‘ 40 MARKETING’. ‘ 54 STATISTICS’. MAJOR-TABLE REDEFINES MAJOR-VALUE. KEY required for binary search 05 MAJORS OCCURS 10 TIMES (ASCENDING or DESCENDING) ASCENDING KEY IS MAJOR-CODE INDEXED BY MAJOR-INDEX. 10 MAJOR-CODE PIC 9(2). 10 EXPANDED-MAJOR PIC X(12). . 23

Figure 12. 11 SEARCH 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. 24

Figure 12. 12 01 MAJOR-VALUE. 05 FILLER. . . 05 FILLER Direct Access to Table Entries PIC PIC X(12) VALUE ‘UNKNOWN’. ‘ACCOUNTING’. ‘UNKNOWN’. ‘BIOLOGY’. PIC 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 25

Figure 12. 13 Grade Point Average 3. 75 3. 50 3. 25 3. 00 2. 75 2. 50 - 4. 00 3. 74 3. 49 3. 24 2. 99 2. 74 Range-step Table Scholarship Percentage 100 75 50 33 25 15 (a) Scholarship Table 26

Figure 12. 13 SCHOLARSHIP-TABLE. 05 GPA-SCHOLARSHIP-PERCENTAGES. 10 FILLER PIC X(6) 10 FILLER PIC X(6) Range-step Table 01 05 VALUE VALUE ‘ 375100’. ‘ 350075’. ‘ 325050’. ‘ 300033’. ‘ 275025’. ‘ 250015’. GPA-TABLE REDEFINES GPA-SCHOLARSHIP-PERCENTAGES OCCURS 6 TIMES INDEXED BY GPA-INDEX. 10 GPA-MINIMUM PIC 9 V 99. 10 SCHOLARSHIP-PCT PIC 999. . 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 27

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 28
- Slides: 28