Emphasis on: Arrays, Character Input, Arithmetic, Loops, and Decisions

Most values which consist of a sequence of digits, such as credit card numbers and 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 credit card or bar code reader. The reader compares the check digit in the transmitted number to a digit calculated by a method similar to this, which treats the sequence of digits as an array containing one digit per element:

1. Add up all the digits with odd-numbered subscripts (positions 1, 3, ...) in the array, excluding the check digit.

For example, for the sequence 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 the same sequence 1347830,
2*1 = 2 and 2*4 = 8 and 2*8 = 16
16 is > 9, so calculate 1+6 = 7
Then 13 (from step 1) + 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 the same sequence 1347830,
30 / 10 = 3 with remainder 0
10 - 0 is 10; 10 is not < 10; therefore the check digit should be 0.
So the check digit for this sequence is correct.

Write a program which inputs sequences of digits (one sequence per line with no spaces between the digits) from the data file DigitSeq.txt. You can assume that a previous program has validated the data to make sure that each sequence consists only of valid digits. Print the input sequence to the screen or to a file, then print the value of what you calculate the check digit should be. Then print VALID or INVALID to indicate whether the check digit is correct or not. The digit sequences may be of different lengths, but you may assume that there will be no sequence longer than 20 characters.

Requirements:

1) The sequence must be stored and manipulated as an array or string.

2) You must use at least 3 functions in addition to main. Some suggestions:

  • input the sequence,
  • convert a character into an integer,
  • do step 1 calculations,
  • do step 2 calculations,
  • do step 3 calculations,
  • determine the correct check digit (this function could call the previous three functions) and return the calculated check digit.

3) At least one function must return a value with a return statement, at least one function must have a value parameter, and at least one function must have a reference parameter.

4) I recommend printing the results of your calculations so that you can code a little at a time and make sure that what youve coded so far is correct. This will also make is easier for me to figure out where you went wrong if an answer is not correct,

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.