# Lab 5 Barcode validator Solution

Lab 5

Barcode validator

Objectives

Learn how to use 1D arrays

Practice converting an algorithm to code

Practice self guided program design

Overview

For this lab, you will write a program that prompts the user to enter the 12 digits of a UPC barcode, which you will store in an array. Using the algorithm described below, you will determine if the barcode is valid or invalid.

Algorithm

A barcode scanner for Universal Product Codes (UPC) verifies the 12-digit code scanned by comparing the code's last digit (called a check digit) to its own computation of the check digit from the first 11 digits. The process of validating a barcode is described below.

Calculate the sum of the digits in the odd-numbered position (first, third, ..., eleventh). Multiply this sum by three.

NOTE: In C arrays, we count array indices starting at 0. Therefore, the "first" digit of the barcode will be stored in the 0th position of your array. Be careful to manage your array indices correctly.

Calculate the sum of the digits in the even numbered positions (second, fourth, ..., tenth) of the barcode.

NOTE: Remember, the 12th digit of the barcode is the checksum. Make sure not to include it in your sum of even indices.

Add these two sums together.

From this combined sum, extact the last digit. If the last digit is a 0, then 0 is the check digit. Otherwise, subtract the last digit from 10 to calculate the check digit.

NOTE: To extract an individual digit from a number, use the % modulus operator

If the check digit matches the final digit of the 12-digit UPC, the UPC is valid. If not, the UPC is invalid.

Program

Write a program that prompts the user to enter the 12 digits of a barcode, separated by spaces. Your program will store the digits in an integer array, calculate the check digit, and compare your calculated check digit to the final barcode digit (the true check digit). If the digits match, the barcode is valid. Otherwise the barcode is invalid. You will print out your intermediate calculations for each step (1-5). See the Example Execution section to determine how to format your results.

TIP: Practice good program design by splitting your program into subtasks and write a functoin to solve each subtask. Do not write all of your code in your main function.

Example Execution

[esus] $ ./lab5

Enter a bar code to check. Separate digits with a space

0 7 9 4 0 0 8 0 4 5 0 1

You entered the code: 0 7 9 4 0 0 8 0 4 5 0 1

STEP 1: Sum of odds times 3 is 63

STEP 2: Sum of the even digits is 16

STEP 3: Total sum is 79

STEP 4: Calculated check digit is 1

STEP 5: Check digit and last digit match

Barcode is VALID.

[esus] $ ./lab5

Enter a bar code to check. Separate digits with a space

0 2 4 0 0 0 1 6 2 8 6 0

You entered the code: 0 2 4 0 0 0 1 6 2 8 6 0

STEP 1: Sum of odds times 3 is 39

STEP 2: Sum of the even digits is 16

STEP 3: Total sum is 55

STEP 4: Calculated check digit is 5

STEP 5: Check digit and last digit do not match

Barcode is INVALID.

Compile & Test

Compile your program using this gcc command. c99 is a shortcut for running gcc -std=c99, which uses the C99 standard instead of the default C89 standard.

$ c99 -Wall lab5.c -o lab5

Self Check

One you finish, you can test out your program with the following barcodes.

0 7 9 4 0 0 8 0 4 5 0 1 Valid

0 1 1 1 1 0 8 5 6 8 0 7 Valid

0 5 1 0 0 0 1 3 8 1 0 1 Valid

0 2 4 0 0 0 1 6 2 8 6 0 Invalid

These barcodes are provided to allow you to verify that your program is working correctly.

Resources

You should attend the Friday Lab session to seek assistance from the TAs and CAs.

For specific questions, attend the weekly lab session or attend the TA's office hours.

You are encouraged to use resources or tutorials on the internet to learn unix or C. Check the class resource list for some links to resources.

Submission

Assigned: February 29th

Lab Day: March 4th

Due Date: March 7th, 8:00am

Make sure you included ample and informative comments - it is 20% of your grade!

Rename your C file to last_first_lab5.c and substitute your last and first name.

NOTE: If you fail to follow the above file naming conventions, your program cannot be graded automatically and you will lose significant points.

Submit your .c file to the D2L Dropbox for Lab 5.

NOTE: Submit only your C file to D2L. Do not submit your object file or executable program. Do not archive (e.g. zip) your file.

Each student will complete and submit this assignment individually. Submit your work on or before the deadline as late work will receive a 50% penalty. Labs submitted more than 24 hours late will not be accepted.

Barcode validator

Objectives

Learn how to use 1D arrays

Practice converting an algorithm to code

Practice self guided program design

Overview

For this lab, you will write a program that prompts the user to enter the 12 digits of a UPC barcode, which you will store in an array. Using the algorithm described below, you will determine if the barcode is valid or invalid.

Algorithm

A barcode scanner for Universal Product Codes (UPC) verifies the 12-digit code scanned by comparing the code's last digit (called a check digit) to its own computation of the check digit from the first 11 digits. The process of validating a barcode is described below.

Calculate the sum of the digits in the odd-numbered position (first, third, ..., eleventh). Multiply this sum by three.

NOTE: In C arrays, we count array indices starting at 0. Therefore, the "first" digit of the barcode will be stored in the 0th position of your array. Be careful to manage your array indices correctly.

Calculate the sum of the digits in the even numbered positions (second, fourth, ..., tenth) of the barcode.

NOTE: Remember, the 12th digit of the barcode is the checksum. Make sure not to include it in your sum of even indices.

Add these two sums together.

From this combined sum, extact the last digit. If the last digit is a 0, then 0 is the check digit. Otherwise, subtract the last digit from 10 to calculate the check digit.

NOTE: To extract an individual digit from a number, use the % modulus operator

If the check digit matches the final digit of the 12-digit UPC, the UPC is valid. If not, the UPC is invalid.

Program

Write a program that prompts the user to enter the 12 digits of a barcode, separated by spaces. Your program will store the digits in an integer array, calculate the check digit, and compare your calculated check digit to the final barcode digit (the true check digit). If the digits match, the barcode is valid. Otherwise the barcode is invalid. You will print out your intermediate calculations for each step (1-5). See the Example Execution section to determine how to format your results.

TIP: Practice good program design by splitting your program into subtasks and write a functoin to solve each subtask. Do not write all of your code in your main function.

Example Execution

[esus] $ ./lab5

Enter a bar code to check. Separate digits with a space

0 7 9 4 0 0 8 0 4 5 0 1

You entered the code: 0 7 9 4 0 0 8 0 4 5 0 1

STEP 1: Sum of odds times 3 is 63

STEP 2: Sum of the even digits is 16

STEP 3: Total sum is 79

STEP 4: Calculated check digit is 1

STEP 5: Check digit and last digit match

Barcode is VALID.

[esus] $ ./lab5

Enter a bar code to check. Separate digits with a space

0 2 4 0 0 0 1 6 2 8 6 0

You entered the code: 0 2 4 0 0 0 1 6 2 8 6 0

STEP 1: Sum of odds times 3 is 39

STEP 2: Sum of the even digits is 16

STEP 3: Total sum is 55

STEP 4: Calculated check digit is 5

STEP 5: Check digit and last digit do not match

Barcode is INVALID.

Compile & Test

Compile your program using this gcc command. c99 is a shortcut for running gcc -std=c99, which uses the C99 standard instead of the default C89 standard.

$ c99 -Wall lab5.c -o lab5

Self Check

One you finish, you can test out your program with the following barcodes.

0 7 9 4 0 0 8 0 4 5 0 1 Valid

0 1 1 1 1 0 8 5 6 8 0 7 Valid

0 5 1 0 0 0 1 3 8 1 0 1 Valid

0 2 4 0 0 0 1 6 2 8 6 0 Invalid

These barcodes are provided to allow you to verify that your program is working correctly.

Resources

You should attend the Friday Lab session to seek assistance from the TAs and CAs.

For specific questions, attend the weekly lab session or attend the TA's office hours.

You are encouraged to use resources or tutorials on the internet to learn unix or C. Check the class resource list for some links to resources.

Submission

Assigned: February 29th

Lab Day: March 4th

Due Date: March 7th, 8:00am

Make sure you included ample and informative comments - it is 20% of your grade!

Rename your C file to last_first_lab5.c and substitute your last and first name.

NOTE: If you fail to follow the above file naming conventions, your program cannot be graded automatically and you will lose significant points.

Submit your .c file to the D2L Dropbox for Lab 5.

NOTE: Submit only your C file to D2L. Do not submit your object file or executable program. Do not archive (e.g. zip) your file.

Each student will complete and submit this assignment individually. Submit your work on or before the deadline as late work will receive a 50% penalty. Labs submitted more than 24 hours late will not be accepted.

You'll get 1 file (1.7KB)