Spring 2005
Due: Friday, 4 Feb
Emphasis on: Review of Arrays, Character Input, Arithmetic,
Loops, and Decisions
The bar codes used on most
products include a check digit (the last digit in the bar code), which allows
checking for transmission errors by the bar code reader. The bar code reader compares the check digit
in the bar code to a digit calculated by the following method:
1. Add up all the array values with odd-numbered
subscripts (positions 1, 3, ...) in the bar
code, excluding
the check digit.
For example, using bar code 1347830, you
would calculate 3
+ 7 + 3 = 13
2. Multiply each array value with an
even-numbered subscript, excluding the check digit, by 2.
If the result of the multiplication
exceeds 9, add its two digits. Add the
sum of these
results to the
sum calculated in Step 1.
For example, using bar code 1347830,
2*1 = 2 and
2*4 = 8 and 2*8 = 16
16 is > 9, so calculate 1+6 = 7
Then 13 + 2 + 8 + 7 =
30.
3. Divide the result of Step 2 by 10 and
subtract the remainder from 10. If the
result is less
than 10, the
result should equal the check digit.
Otherwise, the check digit should be zero.
For example, using bar code 1347830,
30 / 10 = 3 with remainder 0
10 - 0 is 10; 10 is not < 10; therefore
the check digit should be 0.
So this bar code is correct.
Write a program which
inputs bar codes from the keyboard. Read
each digit as a character and, if it is a valid digit, convert the character to
its corresponding integer value and store it as an element in an integer
array. Echo print
the bar code back to the screen. To help
with your debugging and mine, print the results of the calculations in steps 1
– 3 from the instructions above. Then print CORRECT or WRONG to indicate whether the check digit is
correct or not.
You may assume that there
will be no bar code longer than 20 characters.
Requirements:
1) The bar code must be stored and manipulated
as an array or string.
2) You must use at least 3 functions in addition
to main. Some suggestions:
3) At least one function must return a value
with a return statement; at least one function
must have a
parameter, and at least one function must have a reference parameter.
4) Be sure to explain to the user exactly what
format the program expects for its
input.
Extra Credit
For an
extra 10%:
If you
find an invalid character (non-digit) anywhere in the code, prompt the user to
re-enter the entire code and start over with its processing.
Use the following bar
codes for data. I have put blanks
between the digits so that the
numbers here are easier to read, but in your program the
numbers should be input without
any blank spaces.
3 5 0 4 6 8 6 1 4 6 2 2
1 2 3 4 5 6 7 8 9 7
9 3 8 6 3 0 1 3 3
9 6 7 5 8 3 7 8 6 5 7 5 4
5 1
3 0 0
2 3 4 9 3
4 7 3 4 0 1 1 8 3 6 4 1
8 5 7 6 8 9 5 4 3 5 4 5
0
9 0 8 7 8 7 8 3 4 5 0 0 0 2 8
4 8 9 0 2 1
1 2 3 4 5 6 7 8 9