Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program in C that checks if a UPC product code is correct or not. This program is fully specified in the boilerplate code

Write a program in C that checks if a UPC product code is correct or not.

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

This program is fully specified in the boilerplate code provided to you to start with. Read the comments in the code and incrementally work towards a complete program. #include int main(int argc, char **arg) { long long upc; // TODO: Declare additional variables here. /* Have the user enter a UPC code. A UPC code is a numerical code with up to 12 digits printed below the barcode of a product sold in the United States. We need to be careful and use a long long integer variable as 12 digits of decimal numbers need more than 32 bits, which is the length of an int variable. */ printf("Please enter a UPC code with up to 12 digits: "); scanf("%11d", &upc); /* Check if the user entered a positive number with up to 12 digits if ((((long long) oll) digit 4 continue with 166 Step 2: 166 / 10 = 16, remainder 6 => digit 6 continue with 16 Step 3: 16 / 10 = 1, remainder 6 => digit 6 continue with 1 Step 4: 1 / 10 = 0, remainder 1 => digit 1 continue with a The number is now zero, we can stop. Use a temporary variable, so that the UPC code entered by the user is left intact in the upc variable. While breaking the code into digits, count the digits, to get the number of each digit. Start counting with 1. On two variables, sum the odd numbered digits and the even numbered digits. Example: Take the UPC code for "Pero Instant Natural Beverage" The code is 028000596101. Digit 1 is 1 => sumOdd becomes 1 Digit 2 is => sumEven becomes Digit 3 is 1 => sumOdd becomes 2 Digit 4 is 6 => sumEven becomes 6 Digit 5 is 9 => sumOdd becomes 11 Digit 6 is 5 => sumEven becomes 11 Digit 7 is => sumOdd becomes 11 Digit 8 is 0 => sumEven becomes 11 Digit 9 is => sumOdd becomes 11 Digit 10 is 8 => sumEven becomes 19 Digit 11 is 2 => sumOdd becomes 13 Digit 12 is @ => sumEven becomes 19 Done. You can use regular int variables for each digit (0-9) and for the sums sumEven and sumOdd (0-54). At the end of the while loop, you end up with two values, the sum of the even numbered digits and the sum of the odd numbered digits. Now multiply the sum of even numbered digits by 3. Add that product to the sum of the odd numbered digits. Put that number into a variable, e.g. checksum. You can use a regular int variable (0-216). In the example above (for the "Pero Beverage") we get 19 * 3 + 13 = 57 + 13 = 70. If the checksum variable contains a number that ends in digit o (i.e. if it is 10, 20, 30, 40, 50, .., 210) the UPC code is valid, i.e. it corresponds to a product sold somewhere. If the checksum variable contains a number that ends in some other digit (1-9), the code is incorrect. In the example above (for the "Pero Beverage") we get O as the last digit of the checksum, which was 70. So the UPC code of that product is valid (as we would expect for a real product on the market. ) Each laser scanner check register does this computation each time it scans a product, to make sure that the product's bar code got scanned in correctly. If the UPC code is valid, print out a message that the code is valid. Otherwise print out a message that the code is invalid. Refer to the input/output examples on Blackboard for the wording of the different messages. // TODO } else { /* Here the code is not positive or longer than 12 digits. Print an error message. Refer to the input/output examples on Blackboard for the wording of the message. */ // TODO } return ; } Example 1: Please enter a UPC code with up to 12 digits: 028000596101 The code you entered, 28000596101, is a valid UPC code. Example 2: Please enter a UPC code with up to 12 digits: 017324004355 The code you entered, 17324004355, is a valid UPC code. Example 3: Please enter a UPC code with up to 12 digits: 9781101951460 The code you entered, 9781101951460, is incorrect as it is negative or has too many digits. Example 4: Please enter a UPC code with up to 12 digits: -17 The code you entered, -17, is incorrect as it is negative or has too many digits. Example 5: Please enter a UPC code with up to 12 digits: 123456789097 The code you entered, 123456789097, does not correspond to a valid UPC product code

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

=+ what roles should government play in them ?

Answered: 1 week ago