Chapter 12 Table Lookups - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Chapter 12 Table Lookups

Description:

Numeric 0 - 9 101 = 10 102 = 100 10n. Alphabetic A - Z 261 = 26 262 = 676 26n ... Consecutive numeric codes. Direct access to table elements. 7. Figure 12.3 ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 29
Provided by: DavidT128
Learn more at: http://courses.cs.vt.edu
Category:

less

Transcript and Presenter's Notes

Title: Chapter 12 Table Lookups


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

2
Figure 12.1 Table of Major Codes
02 ART HISTORY04 BIOLOGY19 CHEMESTRY21 CIVIL
ENGINEERING24 COMP INF SYS32 ECONOMICS39 FINANC
E43 MANAGEMENT49 MARKETING54 STATISTICS
3
TABLE 12.1 Types of Table Codes
CODE TYPE USED 1 POSITION 2
POSITIONS n POSITIONS
Numeric 0 - 9 101 10 102 100
10n Alphabetic A - Z 261 26 262 676
26n Alphanumeric A - Z, 0 - 9 361 36
362 1,296 36n
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

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.2 Sequential Table Lookup
39
1st try 2nd try 3rd try 4th try 5th try 6th
try 7th try
Match
FINANCE
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.3 Binary Lookup
39
1st try
Match
3rd try
FINANCE
2nd try
8
TABLE 12.2 Required Number of Comparisons for
Binary Search
8 - 15 16 - 31 32 - 63 64 - 127 128 - 255 256 -
511 512 - 1023 1024 - 2047 2048 - 4095
(less than 24) (less than 25) (less than
26) (less than 27) (less than 28) (less than
29) (less than 210) (less than 211) (less than
212)
4 5 6 7 8 9 10 11 12
9
Figure 12.4 Positional Organization Direct
Lookup
02
ART HISTORY
04
BIOLOGY
. . .
19
CHEMESTRY
39
21
CIVIL ENGINEERING
24
COMP INF SYS
. . .
32
ECONOMICS
. . .
1st try
Match
39
FINANCE
FINANCE
. . .
43
MANAGEMENT
. . .
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
Figure 12.5 Initialization via Hard Coding
01 MAJOR-VALUE. 05 FILLER PIC X(10) VALUE
02ART HISTORY. 05 FILLER PIC X(10) VALUE
04BIOLOGY. 05 FILLER PIC X(10) VALUE
19CHEMISTRY. 05 FILLER PIC X(10) VALUE
21CIVIL ENG. 05 FILLER PIC X(10) VALUE
24COMP INF SYS. 05 FILLER PIC X(10) VALUE
32ECONOMICS. 05 FILLER PIC X(10) VALUE
39FINANCE. 05 FILLER PIC X(10) VALUE
43MANAGEMENT. 05 FILLER PIC X(10) VALUE
40MARKETING. 05 FILLER PIC X(10) VALUE
54STATISTICS. 01 MAJOR-TABLE REDEFINES
MAJOR-VALUE. 05 MAJORS OCCURS 10
TIMES. 10 MAJOR-CODE PIC 9(2). 10
EXPANDED-MAJOR PIC X(12).
12
Figure 12.6 Table Initialization (Storage
Schematic)
13
Figure 12.7 Input-Loaded Table
FD MAJOR-CODE-FILE. RECORD CONTAINS 14
CHARACTERS DATA RECORD IS MAJOR-CODE-RECORD.0
1 MAJOR-CODE-RECORD. 05 INCOMING-FILE-CODE P
IC 9(2). 05 INCOMING-FILE-NAME PIC 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-CODE PIC 9(2). 10
EXPANDED-MAJOR PIC X(12). 01 NUMBER-OF-MAJORS PI
C 99 VALUE ZERO. 01 END-OF-MAJOR-FILE-SW PIC
X VALUE SPACES. 88 END-OF-MAJOR-FILE VALUE
Y.
(a) Data Division entries
14
Figure 12.7 Input-Loaded Table
PROCEDURE DIVISION.OPEN INPUT MAJOR-CODE-FILE.PE
RFORM VARYING MAJOR-INDEX FROM 1 BY 1 UNTIL
MAJOR-INDEX gt 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-READEND-PERFORM.IF
MAJOR-INDEX gt 10 DISPLAY MAJOR TABLE TOO
SMALLEND-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
  • Whats 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
Figure 12.8 Input-Loaded Tables
19CHEMISTRY
04BIOLOGY
02ART HISTORY
02
ART HISTORY
04
BIOLOGY
19
CHEMISTRY
EXPANDED-MAJOR (1)
EXPANDED-MAJOR (2)
EXPANDED-MAJOR (3)
MAJOR-CODE (1)
MAJOR-CODE (2)
MAJOR-CODE (3)
19
Figure 12.9 Sequential Lookup with PERFORM VARYING
WORKING-STORAGE SECTION.01 TABLE-PROCESSING-ELEM
ENTS. 05 WS-MAJOR-SUB PIC S9(4) COMP.
05 WS-FOUND-MAJOR-SWITCH PIC X(3) VALUE NO.
05 WS-END-OF-TABLE-SWITCH PIC X(3) VALUE
NO. 01 MAJOR-VALUE. 05 FILLER PIC
X(10) VALUE 02ART HISTORY. 05 FILLER PIC
X(10) VALUE 04BIOLOGY. 05 FILLER PIC
X(10) VALUE 19CHEMISTRY. 05 FILLER PIC
X(10) VALUE 21CIVIL ENG. 05 FILLER PIC
X(10) VALUE 24COMP INF SYS. 05 FILLER PIC
X(10) VALUE 32ECONOMICS. 05 FILLER PIC
X(10) VALUE 39FINANCE. 05 FILLER PIC
X(10) VALUE 43MANAGEMENT. 05 FILLER PIC
X(10) VALUE 40MARKETING. 05 FILLER PIC
X(10) VALUE 54STATISTICS. 01 MAJOR-TABLE
REDEFINES MAJOR-VALUE. 05 MAJORS OCCURS 10
TIMES. 10 MAJOR-CODE PIC 9(2). 10
EXPANDED-MAJOR PIC X(12).
20
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 gt 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.10 SEARCH Statement (Sequential Lookup)
01 MAJOR-VALUE. 05 FILLER PIC X(10) VALUE
02ART HISTORY. 05 FILLER PIC X(10) VALUE
04BIOLOGY. 05 FILLER PIC X(10) VALUE
19CHEMISTRY. 05 FILLER PIC X(10) VALUE
21CIVIL ENG. 05 FILLER PIC X(10) VALUE
24COMP INF SYS. 05 FILLER PIC X(10) VALUE
32ECONOMICS. 05 FILLER PIC X(10) VALUE
39FINANCE. 05 FILLER PIC X(10) VALUE
43MANAGEMENT. 05 FILLER PIC X(10) VALUE
40MARKETING. 05 FILLER PIC X(10) VALUE
54STATISTICS.01 MAJOR-TABLE REDEFINES
MAJOR-VALUE. 05 MAJORS OCCURS 10 TIMES
INDEXED BY MAJOR-INDEX. 10 MAJOR-CODE PIC
9(2). 10 EXPANDED-MAJOR PIC X(12).. . .
INDEXED BY clause required in table definition
22
Figure 12.10 SEARCH 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-MAJOREND-SEARCH.
SET statement establishes starting point
23
Figure 12.11 SEARCH ALL Statement (Binary Lookup)
01 MAJOR-VALUE. 05 FILLER PIC X(10) VALUE
02ART HISTORY. 05 FILLER PIC X(10) VALUE
04BIOLOGY. 05 FILLER PIC X(10) VALUE
19CHEMISTRY. 05 FILLER PIC X(10) VALUE
21CIVIL ENG. 05 FILLER PIC X(10) VALUE
24COMP INF SYS. 05 FILLER PIC X(10) VALUE
32ECONOMICS. 05 FILLER PIC X(10) VALUE
39FINANCE. 05 FILLER PIC X(10) VALUE
43MANAGEMENT. 05 FILLER PIC X(10) VALUE
40MARKETING. 05 FILLER PIC 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-CODE PIC 9(2). 10
EXPANDED-MAJOR PIC X(12).. . .
KEY required for binary search (ASCENDING or
DESCENDING)
24
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-MAJOREND-SEARCH.
25
Figure 12.12 Direct Access to Table Entries
26
Figure 12.13 Range-step Table
Grade Point Scholarship Average
Percentage 3.75 - 4.00 100 3.50 -
3.74 75 3.25 - 3.49 50 3.00 -
3.24 33 2.75 - 2.99 25 2.50 -
2.74 15
(a) Scholarship Table
27
Figure 12.13 Range-step Table
01 SCHOLARSHIP-TABLE. 05 GPA-SCHOLARSHIP-PERCE
NTAGES. 10 FILLER PIC X(6) VALUE
375100. 10 FILLER PIC X(6) VALUE
350075. 10 FILLER PIC X(6) VALUE
325050. 10 FILLER PIC X(6) VALUE
300033. 10 FILLER PIC X(6) VALUE
275025. 10 FILLER PIC X(6) VALUE
250015. 05 GPA-TABLE REDEFINES
GPA-SCHOLARSHIP-PERCENTAGES OCCURS 6 TIMES
INDEXED BY GPA-INDEX. 10 GPA-MINIMUM
PIC 9V99. 10 SCHOLARSHIP-PCT PIC
999.. . . SET GPA-INDEX TO 1.
SEARCH GPA-TABLE AT END MOVE ZERO TO
SCHOLARSHIP-AWARD WHEN STUDENT-GPA gt
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
Write a Comment
User Comments (0)
About PowerShow.com