Homework 9: The UPC Bar Code - Reading and checking UPC barcodes

Handed out: 03/20/07
Due: by 10pm on 03/26/07

Here are the rules for UPC barcodes:
  1. Barcodes consist of black and white vertical lines of 4 possible thicknesses.
  2. The four thicknesses, from thinnest to thickest are 1, 2, 3, and 4.
  3. The barcode begins and ends with 3 thinnest vertical bars(with colors black,white,black). Let's call this sequence "the margin" (technically, they are called respectively "left guard bar" and "right guard bar")
  4. In the middle of the barcode are 5 thinnest lines, in sequence, white, black, white, black, white. Let's call this sequence "the middle" (technically it is called "central guard bar").
  5. Between the beginning margin and the middle is the product manufacturer id. Between the middle and the end margin is the item id.
  6. The lines in the manufacturer id (and the same for the item id) are grouped to represent digits. There are in total 12 digits, 6 in the manufacturer id, 6 in the item id. The codes used to represent the digits in the manufacturer id are
    	     Digit  Thickness	Colors
    	     0		3211	WWWBBWB
    	     1		2221	WWBBWWB
    	     2		2122	WWBWWBB
    	     3		1411	WBBBBWB
    	     4		1132	BWBBBWW
    	     5		1231	WBBWWWB
    	     6		1114	WBWBBBB
    	     7		1312	WBBBWBB
    	     8		1213	WBBWBBB
    	     9		3112	WWWBWBB
    
    You will have noticed that all digits have 4 lines and occupy the same amount of space: if you sum the thicknesses, you always get 7. You will have also noticed that these codes, seen as strings, differ at least at two positions. For example WWBBWWB differs from WWBWWBB at 2 positions, WWBBWWB differs from WBBBBWB at 2 positions, WWBWWBB differs from WBBBBWB at 4 positions, etc. Thus if we make a mistake judging the width of a line, at least if we are wrong by just one thickness, we will be able to detect that we have made a mistake.
  7. The codes used to represent the digits in the item id are the same, but now the colors are reversed. So '0' becomes "BBBWWBW", '1' becomes "BBWWBBW", etcetera.
  8. The last digit of the item id is not really part of the item it, it is a check digit used to determine if there was a read error when scanning the barcode. Thus in the barcode we have: the start margin (BWB), the manufacturer id (6 digits), the middle (WBWBW), the item id (5 digits), the check digit, and the end margin (BWB). The check digit is computed as follows:
    Step 1: Add the digits at odd positions together (the first digit is at position 1)
    Step 2: Multiply this sum by 3
    Step 3: Add the digits at even positions together
    Step 4: Add results of steps 2 and 3.
    Step 5: Subtract this sum from the next multiple of 10: this is the check digit
    For example, if we are given the barcode 036000291452 the computation becomes: sum1 = (0+6+0+2+1+5)*3 = 42; sum2 = 3+0+0+9+4 = 16. sum1+sum2 = 58 Thus the check digit is 60-58 = 2 as in fact is the case (the 12th digit of the barcode is 2).
    As another example, for the barcode 692771981048 the computation becomes: sum1 = (6+2+7+9+1+4)*3 = 87; sum2 = 9+7+1+8+0 = 25. sum1+sum2 = 112. Thus the check digit is 8 (i.e 120-112).
    As a final example, if we are given the barcode 639382000393, the computation becomes: sum1 = (6+9+8+0+0+9)*3 = 96; sum2 = 3+3+2+0+3 = 11; sum1+sum2 = 117. Thus the check digit is 3 (i.e. 120-117).
Here are two barcodes:

Additional information about UPC barcodes can be found at http://en.wikipedia.org/wiki/Universal_Product_Code and at http://electronics.howstuffworks.com/upc1.htm

Your program should in a loop prompt the user to enter a barcode in decimal representation (something like 036000291452 above). The program should in correspondence indicate if the check digit is correct. If not it should print out the correct check digit. Then if correct it should convert the decimal representation to a color representation - in the case of 036000291452 the color representation is
BWBWWWBBWBWBBBBWBWBWBBBBWWWBBWBWWWBBWBWWWBBWB
WBWBWBBWBBWWBBBWBWWBBWWBBWWBWWWBBBWWBBBWBBWBBWWBWB
print that color representation, then convert it back to a decimal representation. Of course you should get back the original decimal representation.

Email your program to the Teaching Assistant together with documentation: requirements, analysis. design and algorithms, implementation, and testing information