Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Bar Code Check Sum Algorithm in C++, Assignments of Computer Science

A programming assignment that requires students to implement a bar code check sum algorithm in c++. The assignment covers array processing, functions, loops, and decisions. Students are asked to write functions to input a bar code, calculate the sum of odd-numbered digits, multiply even-numbered digits by 2 and add their digits, and compare the calculated check sum with the last digit of the bar code. The goal is to ensure the accuracy of bar code transmission.

Typology: Assignments

Pre 2010

Uploaded on 08/18/2009

koofers-user-ph2
koofers-user-ph2 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Name:
Sec: 06/15/2006
Programming Assignment #2
Using C++ Arrays and Functions
This programming assignment continues to emphasize the use of arrays and
functions, as well as practicing the use of loops and decisions to process
information in arrays.
The bar codes that you see on, for example, all of the groceries you buy and have
scanned, are a technology that allow for the speedy and automatic labeling and
transmission of product data. The bar codes used in 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 basic idea (algorithm) used in a bar code reader
is that the reader checks this last digit against a digit that is calculated from all of
the real data/digits that the scanner has just read in. If the check digit matches the
calculated check sum, there is a good chance that the bar code was scanned
correctly, otherwise the bar code scanner will give some kind of an error (usually
a beep) to indicate that the product didn't scan correctly.
The use of check sums like this is a common practice for all types of electronic
communication, including information sent over the internet. Check sums allow
for a simple method to increase the reliability of transmission of data, and to
detect when transmissions have been garbled.
In this lab you will simulate some of the basics of a check sum algorithm for a bar
code reader. We will give you the steps for the mathematical calculation of a
check sum, and your job will be to implement these steps and simulate the
scanning, check sum calculation and verification of a scanned bar code.
Objectives
Review the basic array processing in C++, as well as practice writing functions,
passing arrays to functions, and using the basic control structures to process arrays
of data.
Instructions
1. Your first step is to input a single bar code from standard input, to simulate
the scanning of a bar code. Write a function called inputBarCode that
takes an array of integers as its parameter. Bar codes will be input on
pf3
pf4
pf5

Partial preview of the text

Download Bar Code Check Sum Algorithm in C++ and more Assignments Computer Science in PDF only on Docsity!

Name: Sec: 06/15/

Programming Assignment

Using C++ Arrays and Functions

This programming assignment continues to emphasize the use of arrays and functions, as well as practicing the use of loops and decisions to process information in arrays. The bar codes that you see on, for example, all of the groceries you buy and have scanned, are a technology that allow for the speedy and automatic labeling and transmission of product data. The bar codes used in 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 basic idea (algorithm) used in a bar code reader is that the reader checks this last digit against a digit that is calculated from all of the real data/digits that the scanner has just read in. If the check digit matches the calculated check sum, there is a good chance that the bar code was scanned correctly, otherwise the bar code scanner will give some kind of an error (usually a beep) to indicate that the product didn't scan correctly. The use of check sums like this is a common practice for all types of electronic communication, including information sent over the internet. Check sums allow for a simple method to increase the reliability of transmission of data, and to detect when transmissions have been garbled. In this lab you will simulate some of the basics of a check sum algorithm for a bar code reader. We will give you the steps for the mathematical calculation of a check sum, and your job will be to implement these steps and simulate the scanning, check sum calculation and verification of a scanned bar code.

Objectives

Review the basic array processing in C++, as well as practice writing functions, passing arrays to functions, and using the basic control structures to process arrays of data.

Instructions

  1. Your first step is to input a single bar code from standard input, to simulate the scanning of a bar code. Write a function called inputBarCode that takes an array of integers as its parameter. Bar codes will be input on

standard input by entering the individual digits of a bar code, separated by spaces. A terminal token, or special integer, will be used to indicate the end of the bar code. In this case we will use the value 999 t o indicate the end of the bar code. Therefore, a single bar code typed into your program might look like: 1 3 4 7 8 3 0 999 Notice there are spaces between each bar code digit, and the 999 sentinental is used to indicate the end of the input/transmission of the barcode. You will need to use a loop to read in the digits of the bar code, and store them in the array passed in to the inputBarCode function in successive indexes of the array. What will be the stopping condition of your loop? That is to say, how will you know when you need to stop reading digits from standard input and return from your function? Besides the digits that are read in, you will also need to return the number of digits that the bar code contains back to the caller. In the above example, the bar code contains 7 digits. You should pass back the size of the bar code, either as a result of calling the inputBarCode function, or using a reference parameter. Write a main() function where you call you inputBarCode function and test it before proceeding. You might find it useful to write another function, something like printBarCode, that takes and array of bar code digits and simply displays the digits in the array. In this way you can test calling you inputBarCode function and displaying the results to see if you input is working.

  1. Once you have your inputBarCode function working, you will need to write some functions to calculate a check sum from the bar code digits and then compare the check sum to the last digit of the bar code (the check digit). We will break the task down into 3 steps, and ask you to code each step as a separate function. In the first step, you will add up all of the array values with oddnum bered subscripts (positions 1,3, ...) in the bar code, excluding the check digit. For example, using the bar code 1 3 4 7 8 3 0 from above, the odd numbered digits will be the 3, the 7 and the 3. Therefore you would calculate the sum as 3 + 7 + 3 = 13. This first function for calculating the check sum should be called sumOddDigits. It should return the sum as the result of calling it. It will need the bar code, and the number of digits in the bar code, as parameters. Also, remember, that the last digit of the bar code is the check digit, and should not be used in the calculation of

sum and should be compared directly with the passed in bar codes check digit. If the result is equal to 10, then 0 should be used as the check sum instead. The final result of correct or incorrect is determined by comparing the calculated check sum with the passed in check digit to see if they are equal or not. For example, still using our bar code of 1 3 4 7 8 3 0 The results from the previous steps were 13 and 17 respectively The check digit in the bar code is 0 (e.g. the last digit in the bar code). Adding 13 + 17 = 30 The remainder of dividing 30 / 10 is 0 (there is no remainder, it divides evenly by 10, do you remember how to find the remainder of a division between 2 numbers?) We subtract the remainder from 10 10 – 0 = 10 The result is 10 in this case, so we will use 0 as our check sum The check sum of 0 is equal to the check digit 0, so in this case the bar code transmission was correct, and we should return true as the result for our compareCheckSum function.

  1. You now have all of the pieces coded. Your final step is to put them all together to check a bar code. You should call your functions inputBarCode, sumOddDigits, multiplyEvenDigits and compareCheckSum from your programs main function. Display the intermediate results from the summOddDigits and multipleEvenDigits to standard output. Then based on the result from your compareCheckSumFunction, print out an appropriate message, such as “The bar code is CORRECT” or “The bar code is WRONG”, respectively.
  2. For extra credit, can you put the steps in part 5 into a loop, so that after getting a bar code and checking its correctness, the program goes back and inputs and checks another bar code, and keeps doing this? Can you modify your program so that if the user enters an empty bar code (e.g. they simply type 999 w ithout enter any other digits for a new bar code) that the program then stops of its own accord? Input data You may use the following data for test data (each is labeled as CORRECT or WRONG after the code, but CORRECT or WRONG is not actually part of the input).

3 5 0 4 6 8 6 1 4 6 2 2 (CORRECT)

1 2 3 4 5 6 7 8 9 7 (CORRECT)

9 3 8 6 3 0 1 3 3 (WRONG check=4) 9 6 7 5 8 3 7 8 6 5 7 5 4 5 1 (CORRECT) 3 0 0 2 3 4 9 3 (CORRECT) 4 7 3 4 0 1 1 8 3 6 4 1 (WRONG check=4) 8 5 7 6 8 9 5 4 3 5 4 5 0 (WRONG check=2) 9 0 8 7 8 7 8 3 4 5 0 0 0 2 8 (CORRECT) 4 8 9 0 2 1 (CORRECT) 1 2 3 4 5 6 7 8 9 (WRONG check=6)

Program 2 Finished

You have now completed Program 2. As with previous assignments, the final step you should perform when you complete an assignment is to upload your finished assignment to your eCollege account. Go ahead at this point and upload your Program2.cpp source file to eCollege. Once this is done you have successfully completed the programming assignment #2.