CSCI 201 -- Introduction to Algorithm Design -- handout 7 (p. 1) Problem 5 Due November 12 [35 points] In order to speed up the processing of mail, the United States Post Office has requested that we try to use nine digit ZIP codes. These nine digit ZIP codes are written as a five digit and a four digit number separated by a hyphen, as in 28804-3299. These nine digit ZIP codes are then transformed into a machine readable optical bar code. You may have noticed these bar codes on the lower right corner of mail you have received. They consist of a series of long and short bars. For example, the optical bar code for 28804-3299 is |..|.||..|.|..|.||....|..|..|.|..|.||.|..|.|...|.|.| Note that there are 52 vertical bars in the above code. The beginning and ending vertical bars are always long. The middle 50 bars are used to encode 10 digits. (I'll explain why 10 rather than 9 in a minute). Five vertical bars are used for each digit. The code used for each digit is given in the following chart: 1 = ...|| 6 = .||.. 2 = ..|.| 7 = |...| 3 = ..||. 8 = |..|. 4 = .|..| 9 = |.|.. 5 = .|.|. 0 = ||... [You might ponder this for a pattern.] This particular encoding is called a two-of-five code by computer scientists because each digit is encoded with five bars, two of which are long. Thus the optical bar code for 28804-3299 can be decrypted as: | ..|.| |..|. |..|. ||... .|..| ..|.| ..|.| |.|.. |.|.. .|.|. | 2 8 8 0 4 3 2 9 9 5 Now, we can explain the 10th digit, in this case a 5. The tenth digit is a "check digit". It is chosen so that if you add up all ten digits, the resulting number is divisible by ten. For example, 2+8+8+0+4+3+2+9+9+5 equals 50, which is divisible by ten. The machines that sort mail always add up the ten digits of the optical code to make sure the sum is divisible by ten. If it isn't, the machines assume that ZIP code was either improperly encoded or improperly read. Finally, let's get to your assignment. You are to write a program which prompts its users for a nine digit ZIP code and then prints out the appropriate optical code. The nine digit ZIP code will be entered in the USPO-approved notation (i.e., 28804-3299). You may read these two ZIP codes as two integers (i.e., 28804 and -3299) You are expected to use procedures and functions in this assignment! You grade will depend not only on its correctness but on the structure of your solution into useful and appropriate procedures and functions. CSCI 201 -- Introduction to Algorithm Design -- handout 7 (p. 2) You may find the following function useful in writing your solution. FUNCTION CheckDigit(I: INTEGER): INTEGER; (***********************************************************) ( CheckDigit computes the tenth "check" digit in the ) ( optical encoding of ZIP codes used by the USPO. ) ( ) ( Input: A single integer, I, from 0 to 99999999999. ) ( I is the nine-digit ZIP code represented as ) ( a single integer. For example, 28804-3299 should ) ( be input as the number 288043299. ) ( ) ( Output: The check digit, an integer between 0 and 9. ) (***********************************************************) BEGIN VAR Irem: INTEGER ; Isum: INTEGER ; Irem := I; Isum := 0; (* Loop invariant. * Isum + sum-of-digits(Irem) = sum-of-digits(I) * sum-of-digits(X) is result of adding all the digits of X. *) WHILE (Irem > 0) DO BEGIN Isum := Isum + Irem MOD 10 ; Irem := Irem DIV 10 END; CheckDigit := 10 - Isum div 10 END ; Warning: I never tested this function. But it looks OK.