Associate Dean Emeritus and Adjunct Lecturer of Computer Information Systems and Music
Dutchess Community College, Poughkeepsie NY
TABLE OF CONTENTS: PAGE:
1.
2.
3.
4.
5.
6.
Single-level Tables
Bubble Sort
Searching a Table
Multiple-level tables
Sequential Disk Files
COBOL / JCL Connections
2
3
4
5
6
7
7.
8.
9.
10.
11.
12.
13.
14.
Using DITTO
SORT Statement
SORT with PROCEDURES
Sequential File Update
Balanced Line Algorithm
External Subroutines
CALL Statement
Interactive Programming in COBOL
8
9
12
15
18
22
23
24
15. Calculating a Check Digit 25
16. Storing Numbers Efficiently 27
Appendix A. Using the Operator’s Console 28
Appendix B. Course Outline 29
1. SINGLE-LEVEL TABLES
Single level tables.
Single-dimensional tables use one OCCURS clause and can be defined on level numbers
02 thru 49. A table is a continuous area of main memory, such that repeating groups of data in the table may be referenced numerically, by position, without requiring separate data names. In COBOL 85, you may put a VALUE clause on the record that contains a table, such as VALUE ZEROES or VALUE SPACES, to initialize every element.
A table is made up of Elements, each of which has the same description (and therefore the same PIC clause). Elements may be Elementary Items (with a PIC clause) or Group Items (no PIC clause, but containing lower level items with PIC clauses).
Tables are referenced by a Subscript or an Index.
A Subscript must be a positive integer defined in Working-Storage.
An Index is defined along with the table:
OCCURS x times INDEX BY indexname
An Index requires use of a special verb to establish its value:
SET index TO 1
SET index UP BY 1
SET index DOWN BY 3
Note: an index may also be controlled by PERFORM VARYING.
You cannot MOVE or COMPUTE, ADD etc. an index value.
Using REDEFINES to load a table.
COBOL 85:
01 MONTHTABLE
VALUE ‘JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC’.
05 MONTH OCCURS 12 TIMES PIC XXX.
COBOL 74:
01 MONTHTABLE.
05 MONTHVALUES PIC X(36)
VALUE ‘JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC’.
05 MONTH REDEFINES MONTHVALUES OCCURS 12 TIMES PIC XXX.
2. BUBBLE SORT.
Given N items, sort them.
1.
determine which field to use for the sort
2.
set the items in a table
3.
set up a routine to compare the first element to the second, then the second to the third, etc. until the next to last is compared to item number N
4.
if the two adjacent items are out of order, swap them
5.
once through the table like this is “one pass”
6.
do another pass, starting again with items one and two etc.
7.
the number of passes, worst case, is N-1
8.
Better: stop passes after no swaps are made
9.
Swap = exchange; also called an exchange sort
10.
How do you swap in COBOL? Ans: use holding area. a.
A
Hold; B
A; Hold
B b.
Hint: item (x), item (x+1)
This sort is good for a few (maybe 50 or 100) items. Other routines exist that are better for more items.
Sample URLs for visual illustrations of Bubble Sort: http://math.hws.edu/TMCM/java/xSortLab/ http://max.cs.kzoo.edu/~abrady/java/sorting/
3. SEARCHING A TABLE
To use SEARCH, a table must be indexed!
Indexes are generated internally by the computer and execute much faster than a subscript. You may define a table with subscripts and use the subscript for other purposes, like loading or printing the table, but you are required to use the index to
SEARCH.
SEARCH performs a linear (sequential) search.
Format:
SET index TO 1
SEARCH table
[AT END
Imperative statement(s)]
WHEN condition
Imperative statement(s)
[END SEARCH]
A SET statement is required. Normally, SET index TO 1 before the SEARCH, so it begins looking with element one.
The data may be in any order.
Multiple WHEN clauses are permittted, and the WHEN clause may have any relational operator in the condition.
SEARCH ALL performs a binary search.
A binary search is much faster if the data is already sorted. The table is defined with
ASCENDING KEY or DESCENDING KEY as part of the OCCURS clause.
Note that a preceding SET statement is ignored by SEARCH ALL.
A binary search is limited to one WHEN clause, which must be an “equals” condition.
4. MULTIPLE-LEVEL TABLES
A one-dimensional table is a list structure, which may be visualized as a single column of data.
A two-dimensional table is a matrix organized into rows and columns, like a spreadsheet.
Defining a two-dimensional table.
Requires two OCCURS clauses
First is for the Rows, Second is for the Columns.
As with one-dimensional tables, it is never defined on a 01 level.
Using a two-dimensional table.
Requires use of two subscripts (row, column) to identify one element.
Syntax of subscripts: left paren, row, comma, space, column, right paren, period or space
PERFORM VARYING UNTIL AFTER UNTIL can cycle two variables.
Searching a two-dimensional table.
Each dimension must be indexed.
The SEARCH statement searches only one dimension! You must control the other dimension. For example, use PERFORM VARYING for the Row dimension, then
SEARCH each column in that row. AT END is not used.
Three dimensional tables.
A three-dimensional table adds the dimension of a Plane to the Rows and Columns.
Think of a spreadsheet with multiple sheets in the same file, one per year.
Three subscripts or indexes are required to identify one element.
(plane, row, column)
Two subscripts identify a row within one plane.
One subscript identifies an entire plane.
PERFORM VARYING UNTIL AFTER UNTIL can cycle two variables.
More than three dimensions.
COBOL 85 alows up to seven dimensions. COBOL 74 allows up to three.
5. SEQUENTIAL DISK FILES
TAPE – allows sequential access only
DISK – allows sequential access (CIS 123) or random access (CIS 211)
Relationship between COBOL and JCL
Which comes first?
COBOL and JCL for Sequential disk files:
A utility, DITTO, allows you to copy disk data.
$$DITTO XXX gives all DITTO commands and syntax
$$DITTO SPR Sequential disk to PRinter
Students are issued Track numbers on disk
Each track holds about 50,000 characters of data.
See the Lab Assistant if you need more tracks than you were issued.
6. COBOL / JCL CONNECTIONS
SELECT filename
ASSIGN TO SYS012.
FD filename
LABEL RECORDS ARE STANDARD
SYSnnn where nnn = 006 to 099
RECORDING MODE F
[ BLOCK CONTAINS n RECORDS ]
[ RECORD CONTAINS n CHARACTERS ].
01 recordname PIC X(80).
_________________________________________________________________
OPEN INPUT filename
READ filename
OPEN OUTPUT filename
WRITE recordname
AT END INVALID KEY
MOVE “Y” TO EOF-SWITCH DISPLAY “ERROR”
CLOSE filename. CLOSE filename.
_________________________________________________________________
// ASSGN SYS012,243 or // ASSGN SYS012,DISK,VOL=DAP000,SHR
// DLBL SYS012,’my file ID’,0,SD file-ID is maximum 44 characters
// EXTENT SYS012,DAP000,1,0,n,n starting track number, how many
Notes:
// ASSGN, // DLBL and // EXTENT must come before // EXEC
// EXTENT must follow right after the matching // DLBL
Variation using COBOL external name for file name:
SELECT filename
ASSIGN TO SYS012-extname. External name is maximum 7 char.
// ASSGN SYS012,243 or // ASSGN SYS012,DISK,VOL=DAP000,SHR
// DLBL extname,’my file ID’,0,SD
// EXTENT SYS012,DAP000,1,0,n,n
7. USING DITTO
To DITTO a sequential disk file to a printout, add this:
// ASSGN SYS012,etc.
// DLBL SYS012,etc.
// EXTENT SYS012,etc.
// UPSI 1
// EXEC DITTO
$$DITTO SET DATAHDR=NO
$$DITTO SPR FILEIN=SYS012 or $$DITTO SPR FILEIN=extname
$$DITTO EOJ
/*
Notes:
$$DITTO XXX will print a list of all DITTO commands and syntax
You could run DITTO in a short job by itself by adding a JOB card and /& to the code above.
8. SORT STATEMENT
OVERVIEW:
SORT in COBOL is an example of a Macro
Sort a file from card, tape or disk, and create a new file on disk
Flowchart a SORT as a predefined process
Simple form: sort a disk file onto a new disk file
Can use procedure(s) to do processing before and/or after the sort
Requires special coding:
SYS001, JCL, no tracks, external name SORTWK1
SD with no label records
New COBOL verbs: RELEASE and RETURN
SIMPLE SORT
SORT filename
ON ASCENDING/DESCENDING KEY fieldname, …
USING input-filename
GIVING output-filename.
SORT opens the input file, sorts it in the system sort-work area, creates a new output file in sorted order, and closes all files = Macro
Define the Sortwork file with SD (not FD) in FILE SECTION
Record description: make total length of records match use separate fields for sort key(s); rest is filler can have multiple sort keys can combine ascending and descending
EXAMPLE
SORT SORT-FILE
ON ASCENDING KEY EMPLOYEE-NUM
ON DESCENDING KEY EMPLOYEE-AGE
USING UNSORTED-EMPLOYEE-FILE
GIVING SORTED-EMPLOYEE-FILE.
DEBUGGING HINTS
Do not OPEN or CLOSE the sort-work-file
Files used by SORT as input and output must not be OPEN
SORT will open and close them
They make be opened and closed elsewhere in the program
Our SORT at DCC is an unstable sort
Uses EBCDIC collating sequence on DCC mainframe
Possible to code COLLATING SEQUENCE IS
NATIVE (EBCDIC)
STANDARD-1 (ASCII)
Collating Sequence in COBOL:
EBCDIC (mainly mainframes)
Low-values
Space
Special characters
Lower case letters
Upper case letters
Numbers
High-values
ASCII (mainly microcomputers)
Low-values
Space
Special charactrers
Numbers
Upper case letters
Lower case letters
High-values
COBOL - JCL for SORT
SELECT sort-file
ASSIGN TO SYS001-SORTWK1.
Use SYS001 in SELECT and SORTWK1 as external name
No need for DLBL or EXTENT for Sort-work-file
There is a large system sort-work-area already defined
If you choose to define your own sort-work-area, then
SYSLOG LISTING
Use a different external name (or none)
Don’t use SYS001
Use a DLBL and EXTENT for the disk area
Make the area 10% larger than file to be sorted
When SORT executes, look for
INSERT #, DELETE #
IN #, OUT # or
Sample SORT Output in the SYSLOG:
DFSORT/VSE 5746-SM3, VER 3, REL 2.0, PTF 00, DATE 09/13/01
7B47I SORT FIELDS=(0001,0005,ZD,A,0070,0002,ZD,A),WORK=1
7B47I RECORD TYPE=F,LENGTH=(000080,,000080)
7B47I OPTION FILNM=(SYS015,SYS016),SORTIN=016,SORTOUT=015
7B58I SYS015 SYSNO IGNORED
7B58I SYS016 SYSNO IGNORED
7D33I INPFIL BLOCKSIZE = 80 BYTES
7D33I OUTFIL BLOCKSIZE = 80 BYTES
7C10I STORAGE USED = 606320 BYTES
7C02I MODULE STATUS: PARTITION (POSSIBLE PERFORMANCE DEGRADATION)
7J14I SORT CAPACITY APPROX 102000 RECORDS
7J03I SORT COMPLETE, IN 23, OUT 23.
9. SORT WITH PROCEDURES
Possible to do processing before sorting (INPUT PROCEDURE)
Example:
Possible to do processing after sorting (OUTPUT PROCEDURE)
Example:
Possible to do both INPUT and OUTPUT PROCEDURES in same sort.
Using procedures make coding quite a bit more complicated and requires new verbs: RELEASE and RETURN
RELEASE – send record to the sort file (like WRITE)
RETURN – get record back from the sort file (like READ)
Unlike simple sort, you open and close all files used
DEBUGGING SORT WITH PROCEDURES
The INPUT PROCEDURE is a Section (a group of consecutive paragraphs that are given a name)
The OUTPUT PROCEDURE is also a Section (a different group of consecutive paragraphs).
On some compilers, an INPUT or OUTPUT procedure may not reference a paragraph or section outside the procedure
If you are performing a section, it is acceptable to code GO TO in COBOL, to exit a multiparagraph section by a GO TO the last paragraph in that section.
Use RELEASE and RETURN, not WRITE and READ, on the sort-work file
RELEASE sort-record
RELEASE sort-record FROM other-record
RETURN sort-file
RETURN sort-file INTO other-record
Never use RELEASE and RETURN on ordinary FD files
An INPUT PROCEDURE cannot contain a SORT verb
- no nested SORTs
An OUTPUT PROCEDURE cannot contain a SORT verb
When using PROCEDURES, the COBOL program really just uses SORT as the main module. All the processing is done within the sections referenced by the procedures.
Typical use:
SORT
Input procedure does data validation, then
RELEASE sort record (sends fewer records)
Output procedure RETURN sort file
Then do checking for duplicate or blank records (now in order)
SORT has four combinations:
SORT USING GIVING
SORT USING
SORT INPUT PROCEDURE
SORT INPUT PROCEDURE
OUTPUT PROCEDURE
GIVING
OUTPUT PROCEDURE
Procedures in COBOL 74 require:
Section Name
Followed by paragraph name
Section Exit (on some compilers)
Use of GO TO to escape the section, at the end of processing
Procedures in COBOL 85
May use Sections
Can use paragraphs instead
Syntax of RETURN Statement:
RETURN
AT END
Imperative statement(s)
NOT AT END
Imperative statement(s)
[END-RETURN]
10. SEQUENTIAL FILE UPDATE
Files used:
Master File (541)
One master file if random access (disk only)
Old master and New master if sequential access
Transaction File (542)
Also, reports generated
Audit trail (544)
Error listing
Control totals
Updating a sequential master file (543, 544)
Old master, new master and transaction files must be sorted the same way (545)
Master files are built sequentially, sorted on the key field, with no duplicates in the primary key (545)
The key field is described as alphanumeric
will not be processed numerically
need to use HIGH-VALUES or LOW-VALUES (551)
The media for the old and new master must be the same
Rotation scheme
The media for the transaction file could be different
Types of transactions:
Add a new record
Delete an old record (how?)
Change an old record
Errors:
No such record
Impossible action
Out of order
Use Nested IF to test for type of transaction, or EVALUATE (85)
What if record in Old Master is not changed or deleted?
Challenge: multiple transactions on the same master record (556)
Output includes Control Totals
Ex. # transactions read should equal :
+
# transactions rejected
(could subdivide as to why)
# transactions successfully processed (could subdivide)
(processed: added, deleted, changed)
Careful: don’t just add the separate totals to get the grand total; also take the grand total from the number of transactions read, and compare.
How could transactions be rejected as errors?
Add
Delete
Change when record already exists in the old master when record doesn’t exist in the old master when record doesn’t exist in the old master
Other ex. bad code, out of range, out of order
Maybe, if duplicate transaction on same key field
Handling an update with multiple transactions on the same key
Do not immediately write the new master.
Consider this sequence on the same key:
Add
Change
Change
Delete
Ensure the transactions are sorted the same way as the old master, and that multiple transactions on the same key are in order.
If needed, add a field to sequence the transactions.
Is Add followed by Delete the same as Delete followed by Add?
Example of a sequential update with multiple transactions on the same key field:
TRANSACTION FILE OLD MASTER FILE NEW MASTER FILE
1595
1597
1598
1599
1601
1601
1602
1604
1605
1607
1608
1609
1610
D
D
A
C
C
D
A
A
D
A
D
B
C
1597
1600
1601
1604
1606
1611
1615
EOF
EXCEPTION REPORT
1598
EOF
M. K. Finley
CIS 123
1612
1615
EOF
Hints:
A
A
-
1595 no such record to delete
Remember to copy an old master record over to the new master file if it is unchanged.
Remember not to write until you know the result of all transactions on the same key.
Always check for codes that may not be what you expect.
Not A, C or D
Not sorted
In addition to the Exception Report, consider printing a message for the results of every transaction (as an Audit Trail).
?
?
11. BALANCED LINE ALGORITHM is one example of a routine that will handle sequential processing with multiple transactions having the same key field value.
Refer to the Balanced Line Algorithm by line number.
Lines 2 - 5 Read the first transaction and the first old master record.
Note use of figurative constant HIGH-VALUES, which will be higher than any possible key field value. This means that the key field in each record must be described with PIC X( ) not PIC 9( ).
Line 6 Create a working-storage field, PIC X( ), called low-key. This field will always have the lowest key field value, telling you which record to process next.
Note that if the key field values of the next transaction and the next old master are equal, it doesn’t matter which one you move to lowkey.
Line 7
Lines 8 – 12
Loop until there are no more records to handle from either file.
Low-key will only equal high-values when both files reach EOF.
The paragraph to be looped is lines 8 through 24.
If the master key is lower, there is no transaction affecting the old master. Format the new master record (move the old master to the new master) but don’t write it yet. Also set a flag to say this, and read another old master.
Enhancement
If you’re having trouble debugging the logic, add a DISPLAY statement just before line 8 to show the value of low-key.
Lines 13 – 14 The master key is not lower than the next transaction, so set a flag to say this.
Lines 16 – 20 Keep reading transactions and handling them until there are no more that are lower than the next old master. Line 176 performs a paragraph called process-transaction, which is lines 28 – 53.
Lines 21 – 22 Only when you are sure (by the flags) there are no more transactions that could affect the master record, write it to the new master file. Note, this is the only place in the program that the record is written to the new master!
Line 24
Enhancement
Same code as line 6, to establish the next lower record to handle.
After line 25, print control totals.
Lines 28, 34,
42 and 49
Check for the transaction code. Line 49 is there because you do not want to assume there are only three possibilities; the fourth possibility is that the code is invalid. Note: this test for the possible transaction codes could be coded with EVALUATE instead of a nested IF.
Enhancement
Source:
Depending on the transaction code, various flags are set, reports are written and actions are taken. Flags are used to handle situations like no such master to change or delete, or records that can’t be added because they are already there.
For debugging, DISPLAY the value of the transaction code just before line 28.
Line 32 This involves using some of the transaction data to change the values in the new master, which you already moved there from the old master. Some fields in the new master will not be changed.
Line 39 This could involve moving the entire transaction record, or just most of it, to the new master record. If the transaction record contains extra characters such as a sequence field (to sort the transactions chronologically), those extra characters would not be moved to the new master. The transaction update code may also not belong in the new master, depending on how the data is formatted.
Things to remember:
Important: “create” means format/load, not write. No writing to disk takes place in the entire process-transaction paragraph; this is determined in lines 21 and 22.
None of this works if the transactions and the old master file are not sorted the same way.
The field, low-key, is always equal to the lower value of the current transaction key and master key.
The old master file is not changed during the sequential update. A copy, with appropriate changes, is created as the new master file.
This could become the old master file in the next update.
Flags are set so writing to the new master is done in one place.
The Balanced Line Algorithm described here is closest to that described in an article by
Barry Dwyer in the January 1981 issue of Communications of the ACM .
Update-sequential-file module:
1. Open files
2. Read first record in transaction file;
3. at end move HIGH-VALUES to transaction key
4. Read first record in old master file;
5. at end move HIGH-VALUES to master key
6. Move minimum of transaction key and master key to low key
7. PERFORM UNTIL low key = HIGH-VALUES
8. IF master key = low key
9. Move old master record to new master record
10. Move Y to master record available flag
11. Read next record in old master file;
12. at end move HIGH-VALUES to master key
13. ELSE
14. Move N to master record available flag
15. END-IF
16. PERFORM UNTIL transaction key not = low key
17. PERFORM process-transaction module
18. Read next record in transaction file;
19. at end move HIGH-VALUES to transaction key
20. END-PERFORM
21. IF master record available flag = Y
22. Write new master record
23. END-IF
24. Move minimum of transaction key and master key to low key
25. END-PERFORM
26. Close files
27. Stop
Process-transaction module:
28. IF transaction is a change
29. IF master record available flag = N
30. Write report record with error message -
no matching master record exists
31. ELSE
32. Change data in new master record using transaction data
33. END-IF
34. ELSE
35. IF transaction is an addition
36. IF master record available flag = Y
37. Write report record with error message —
master record already exists
38. ELSE
39. Create new master record using transaction record data
40. Move Y to master record available flag
41. END-IF
42. ELSE
43. IF transaction is a deletion
44. IF master record available flag = N
45. Write report record with error message -
no matching master record exists
46. ELSE
47. Move N to master record available flag
48. END-IF
49. ELSE
50. Write report record with error message -
invalid transaction record
51. END-IF
52. END-IF
53. END-IF
LOW
KEY
1595
1597
1598
1599
1601
1601
1602
1604
1605
1607
1608
1609
1610
1612
1615
EOF
Exercise
TRANS
KEY CODE
TRANS
A
D
A
D
B
C
A
A
-
D
D
A
C
C
D
A
CODE
MASTER
KEY
1597
1600
1601
1604
1606
1611
1615
EOF
MASTER
AVAILABLE
FLAG ACTION
12. EXTERNAL SUBROUTINES
Principles
A subroutine is another name for a module. Structured programming technique stresses the use of separate modules for separate functions, to make development and debugging easier.
In COBOL, the most common form of subroutine is a Paragraph. When you
PERFORM a paragraph, the program executes the code in that paragraph and then returns control to the line below the PERFORM statement. A paragraph is an
Internal Subroutine, in that the module is inside your program.
An external subroutine, a module outside your program, can be made a part of your program in a similar way to the way you would execute a paragraph. CALL executes the code in that program and returns control to the line below the CALL statement.
The external subroutine is named the Called program, and the main program with the CALL statement is named the Calling program.
The CALL statement can also pass data to the called program, which can make calculations and pass the data back to the calling program.
The external subroutine does not have to be written in the same language as the calling program. Once written, the external subroutine is compiled and stored on the Relocatable Library (RL) as an Object Module. Programmers often develop libraries of code that can be used as needed. Specialists may work on an external subroutine that is called by many other programs, to avoid duplication of effort.
The external subroutine can be changed without having to recompile any of the programs that call it.
The Linkage Editor “adds” the external subroutine to your program. You can see this by using // OPTION LINK and ACTION MAP (rather than ACTION
NOMAP). The MAP is the Linkage Editor listing of all of the object modules used to make your program run. The main program will be an entry in the MAP, and the external subroutine will be another entry in the MAP.
A CALL statement is flowcharted just like a PERFORM statement.
13. CALL STATEMENT
Format in the Calling program:
CALL “external-subroutine-name”
[USING field or record name, …]
The external-subroutine-name is the PROGRAM-ID of the Called program. Maximum 8 characters. On some systems, this name should not begin with a number.
The USING is optional, and would be needed if data is to be passed between the Calling and Called programs.
In COBOL 74 and some compilers, USING must be followed by a recordname (the 01 level).
In COBOL 85, you may have a fieldname or fieldnames after USING.
You may still pass a recordname.
Format in the Called program:
A LINKAGE SECTION must be used if data is passed between the Calling and
Called programs. The LINKAGE SECTION is not needed if no data is passed.
The LINKAGE SECTION is placed in the DATA DIVISION after the
WORKING-STORAGE SECTION. Can use special 77 level number.
PROCEDURE DIVISION USING … specifies the names of the field(s) or record from the LINKAGE SECTION that will be used for data that is passed.
The names chosen to pass the data do not have to be the same in the Calling and
Called programs. However, the position and data types (PIC clauses) must match.
EXIT PROGRAM is the last statement that is executed in the Called program (not
STOP RUN). EXIT PROGRAM causes control to be transferred back to the
Calling program. In COBOL 74, EXIT PROGRAM (or GOBACK) must be the only statement in the paragraph.
On some compilers, GOBACK may be used instead of EXIT PROGRAM.
Note: Be careful about counters and accumulators when using CALL. Depending on your program’s intent, you may want to zero these out in the Calling program each time you pass new data to the Called program.
14. INTERACTIVE PROGRAMMING IN COBOL
ACCEPT Inputs a record from the default input device, without using a file
Microcomputer: default input device is keyboard; waits until operator presses Enter
Mainframe: default input device is card reader in batch COBOL
DISPLAY Outputs a field or record to the default output device, without using a file
Microcomputer: default output device is screen
Mainframe: default output device is line printer in batch COBOL
Notes: Best to not mix WRITE and DISPLAY statements to the mainframe printer; can cause unexpected line spacing or overprinting
Use DISPLAY on a mainframe in batch COBOL for “quick and dirty” output, not regular processing. Example: unexpected error message when writing to a disk file.
DISPLAY can display literals and/or data from a field or record. Match up the quotes carefully in pairs. Items in quotes are printed as is; items outside quotes are treated as data names and their value will be substituted. Separate entries with a comma, space or just a space. Use of a comma enhances readablility of the line.
Ex: assume item-key-num is now 12345
WRITE old-master
INVALID KEY
DISPLAY “Error writing to disk on”, Item-num, “.”.
Prints: Error writing to disk on 12345.
15. CALCULATING A CHECK DIGIT
A check digit is a calculated number added to the rest of the "real" number. Its value will be computed based on the other digits, using a formula.
Check digits are useful to detect data entry errors or attempt to generate fraudulent numbers.
A common formula used as an example of this concept is Modulus 11.
MODULUS 11
This example assumes the last digit is the check digit.
Write the number. Put the number of the column over the number, working right to left. Ignore the column 1 on the far right, since this is the number we're trying to calculate.
Multiply each column. Sum the resulting numbers. Divide that number by 11.
Subtract the remainder from 11 to get the check digit. Truncate to one digit if needed.
Example using the number 12,345:
5 4 3 2
1 2 3 4 5
Multiply each of the columns separately (except column 1), and add those numbers together.
(5*1) + (4*2) + (3*3) + (2*4) = 30
Divide this sum by 11. All you want is the remainder.
30/11 = 2 with remainder 8
Subtract the remainder from 11.
11 - 8 = 3
The check digit is 3.
The correct number that begins with 1234 must be 12,343.
The number 12,345 fails the test, since it should be 12,343.
The chance of guessing that fifth number correctly is only about 1 in 10.
Example using the number 24,680:
5 4 3 2
2 4 6 8 0
(5*2) + (4*4) + (3*6) + (2*8) = 60
60 / 11 = 5 with remainder 5
11 - 5 = 6 (the check digit)
The correct number should be 24,686.
Example using the number 433,333:
6 5 4 3 2
4 3 3 3 3 3
(6*4) + (5*3) + (4*3) + (3*3) + (2*3) = 66
66 / 11 = 6 with remainder 0
11 - 0 = 11 truncated = 1 (the check digit)
The correct number should be 433,331.
16. STORING NUMBERS EFFICIENTLY
Numeric data in COBOL is normally stored in such a way that one character occupies one storage position, making it possible to compute, print or display the data with no conversion. There may be an application in which it is more efficient to store large positive or negative numbers in a smaller number of positions. For example, storing a very large table of numbers on a disk or tape could benefit from using much less storage space.
COBOL offeres a way to increase the storage of numbers to nearly twice as efficient, by the USAGE clause.
Default:
USAGE IS DISPLAY one character = one position
COBOL 85 only:
USAGE IS PACKED-DECIMAL
“packs” the numbers (see below)
In COBOL 74, most compilers have a substitute available:
USAGE IS COMP-3 or
USAGE IS COMPUTATIONAL-3
In PACKED-DECIMAL or COMP-3, the right-most digit is stored with the sign, and each position to the left stores TWO digits. Thus, the longer the number, the more space is saved. Savings approach 50% for larger numbers.
Examples:
05 Field1 PIC 9(5)
05 Field2 PIC 9(5) USAGE IS DISPLAY
5 storage positions
5 storage positions
05 Field3 PIC 9(5) USAGE IS PACKED-DECIMAL
05 Field4 PIC 9(100) USAGE IS PACKED-DECIMAL
3 storage positions
51 positions
Note: packed data may not be printed or displayed directly on the screen; it must first be moved into a field that is USAGE IS DISPLAY, which is the default. The computer will make the data conversion.
Note: USAGE may also be used on a group item, affecting each field.
01 Record1 USAGE IS PACKED-DECIMAL.
02 Field1 PIC 9(5).
02 Field2 PIC 9(5). Record1 takes 6 storage positions total.
APPENDIX A
Using the operator’s console:
What to look for:
Programs run in partitions. Active partitions usually are F5, F7 and BG
As programs execute, you can see the job steps.
Look for a program with many seconds of CPU time while in PHASE; it may be looping.
Navigating to the correct screen:
Alt N
Alt P switch to the (N)ext screen switch to the (P)revious screen
Press ESC if you went the wrong way
Operator command examples: flush the job running in F5 F F5
5 Delete allow the job in F5 to continue and delete the file that it is trying to write over
To shut down the console:
Press F3, F3, F3 then F4
Tip:
In CMS, type IND and look at the CPU percentage. This figure is a running average of the load on the CPU. When it gets high, someone is looping.
WEEK
1 - Jan 13
TO BE COVERED IN CLASS
PREPARE FOR
CLASS
Course Introduction
Arrays/One Dimensional Tables (Chapter
Read Chapter 12
12)
Assign Assignment 1 - Sales Calculation
Work on Assignment 1 2 - Jan 20 No Class
3 - Jan 27
Bubble sort (Not in Text)
Linear/Binary Search (Chapter 12)
Go over contents for Test #1
Work on Assignment 1
4 - Feb 3
Two Dimensional Tables
Assign Assignment 2 - Class Schedule
5 - Feb 10 Two Dimensional Tables
6 - Feb 17
No Class
Work on Assignment 2
DUE
Work on Assignment 2
Test #1
Study for Test #1
Work on Assignment 2 Assignment 1
7 - Feb 24 Sort Statement (Chapter 14)
Read Chapter 14
Work on Assignment 2
8 - March 3
Sort Statement
Go over contents of Test #2
Work on Assignment 2
9 – March
10
No Class
10 – March Called/Calling Programs (Chapter 16)
17 Assign Assignment 3 - Test Grades
Work on Assignments
Read Chapter 16
Study for Test #2
Work on Assignment 4
Test #2
11 – March
Called/Calling Programs
24
13 - April 7
Sequential Disk File Creation
Work on Assignment 3
12 – March
Sequential Disk File Creation (Chapter
13)
31
Assign Assignment 4 - Master File Build
Read Chapter 13
Work on Assignment 4
Work on Assignment 4 Test #3
14 – April
14
Sequential Disk Update (Chapter 13)
Assign Assignment 5 - Master File
Update
15 – April
21
Sequential Disk Update
16 – April
28
Subprograms and Interactive
Programming
Work on Assignment 5
17 - May 5
18 - May 12
Check Digits and Final Review
FINAL EXAM
Work on Assignment 5
Think about Final Exam
Complete Assignments
2-4
Last chance for
Assign 2-4
Study for Final Final Exam
Complete Assignment 5 Assignment 5