Universal Product Code Analysis
In this assignment you will write a C program to analyze a UPC
See: http://en.wikipedia.org/wiki/Universal_Product_Code for details.
There are a variety of UPC codes.
We will only analyze the 12 digit variety.
A barcode is printed on many items and is scanned in a shop for pricing and
It consists of a sequence of black and white bars of different
The widths encode a sequence of 12 digits.
Here is a barcode for the UPC, 036000291452. As shown, there is a guard
at the start, a guard in the middle, and a guard at the end.
A black bar can be of width 1, 2, 3 or 4 and is represented by '1', '11',
'111' or '1111' Similarly a white bar is represented by '0', '00', '000' or
A valid barcode consists of the left guard, 101, a string of 42 '1's
or '0's representing the left 6 digits, a middle guard, 01010, a string of
42' 1's or '0's representing the right 6 digits, and the right guard, 101.
Each UPC digit is represented by 7 '1's or '0's.
Example: If 3 occurs in the
left 6 digits it is represented by 0111101, meaning a white bar of width 1,
a black bar of width 4, a white bar of width 1 and a black bar of width 1.
If 3 occurs in the right 6 digits it is represented by 1000010, (that is,
the "negative" of 0111101).
Your program will input a barcode from the file,
"barcode.dat". For the above barcode the file barcode.dat contains:
Your program will analyze this and convert it into the UPC code,
0 3 6 0 0 0 2 9 1 4 5 2.
The left values for all the ten digits are given by:
0 = 0001101, 1 = 0011001, 2 = 0010011, 3 = 0111101, 4 = 0100011,
5 = 0110001, 6 = 0101111, 7 = 0111011, 8 = 0110111, 9 = 0001011
These values can be represented by joining them into the string:
Note that the left value corresponding to digit d starts at position
7*d, and has length 7.
Similarly the right values is:
Your program will read a barcode consisting of 95 '0's or '1's, from the
and convert it into a 12 digit UPC. Assume that
there are no errors in the barcode.
Your program should use these functions:
main : read the barcode from barcode.dat, print its left and right parts,
call analyze, and print the resulting UPC digits.
void analyze(char barcode[ ], int upc[ ]): analyze the barcode given by
the first parameter and convert into 12 digits,
in the second parameter.
int convert(char barcode[ ], int n, char code[ ]): convert the 7 barcode
characters starting at position n to a single UPC digit.
The first parameter,
barcode, should match part of the third parameter, code (which will either
be a left value string, or a right value string).
The position of the
match can be used to calculate the UPC digit.
int match (char a[ ], int aStart, char b[ ], int bStart, int n): match 'n'
characters of string a,
starting at index aStart with n characters of
string b starting at index bStart.
If successful, return 1 else
The program should output the final UPC digits. Here is a sample output.
Left part: 0001101 0111101 0111011 0100011 0101111 0101111
Right part: 1110010 1001000 1000010 1110010 1011100 1110100
UPC Code is: 0 3 7 4 6 6 0 8 3 0 4 9[/align]