I need the CORRECT answers for two questions regarding the c

I need the CORRECT answers for two questions regarding the code. Heres the link to the code. https://www.chegg.com/homework-help/questions-and-answers/c-programming-class-please-read-description-suppose-fairly-elementary-code-really-need-cod-q18652102

Here are the questions that I need answers to. Please answer them correctly showing all work. Thank you.

Heres the code for the description:

Description Manufacturers of goods sold in U.S. and Canadian stores have a bar code on each product.This code, known as a Universal Product Code (UPC), identifies both the manufacturer and the product. Each bar code represents a twelve-digit number, which is usually printed underneath the bars. For example the following digits \"0 13800 15173 5\" comes from a package of Stouffer\'s French Bread Pepperoni Pizza. The first digit identifies the type of item (0 or 7 for most items, 2 for items that must be weighed, etc.). The first group of five digits identifies the manufacturer (13800 is the code for Nestle USA\'s Frozen Food Division). The second group of five digits identifies the product (including the package size). The final digit is a \"check digit whose only purpose is to help identify an error in the preceding digits. If the UPC is scanned incorrectly, the first 11 digits won\'t be con- sistent with the last digit, and the store\'s scanner will reject the entire code Here is one method of computing the check digit: Add the first, third, fifth, seventh, ninth, and eleventh digits Add the second, fourth, sixth, eighth, and tenth digits Multiply the first sum by 3 and add it to the second sum Subtract l (one) from the total. Compute the remainder when the adjusted total is divided by 10 Subtract the remainder from 9 Using the Stouffer\'s example we get 0 3 0 1 1 3 8 for the first sum and l 8 0 5 7 21 for the second sum. Multiplying the first sum by 3 and adding the second yields 45. Subtracting 1 (one) gives 44. The remainder upon dividing by 10 is 4. When the remainder is subtracted from 9, the result is 5. You are to write a C program that prompts the user to enter the first 11 digits of the UPC, then it will display the corresponding check digit. The programs keeps prompting the user until she/he enters -1 to indicate ending the interaction and program. See sample interaction given below To avoid errors, the user will be asked to enter the first ll digits in three parts: the first digit at the left, the first group of five digits, and the second group of five digits. In order to separate the digits of the 2 groups, we use a feature of the \"scanf\" function that can separate each digit of a multi-digit integer. When the user enters a -1 the program should end by displaying a \"Program Termination\" message. Sample output of the program Warsaw 302 /a.out Enter the first digit or 1 to terminate 0 Enter first group of 5 digits: 13800 Enter second group of 5 digits: 15173 Check digit 5

Solution

The answer for the first question is :

#include <stdio.h>

void main()
{
  
int a[11],i;
int sum1,sum2,check,c;

while(1)
{
printf(\"Enter first digit enter -1 to terminate\");
scanf(\"%d\",&a[0]);
  
if(a[0]==-1){break;
  
}
else
{ printf(\"\ Enter 5 digits\");
for(i=1;i<6;i++){
scanf(\"%1d\",&a[i]);
}
printf(\"\ Enter second 5 digits\");
for(i=6;i<11;i++){
scanf(\"%1d\",&a[i]);
}
printf(\"\ Enter the 12 th digit\");
scanf(\"%d\",&c);
  
  
}
sum1=0;
for(i=0;i<11;i+=2)
{
sum1+=a[i];
}
  
sum2=0;
  
for(i=1;i<11;i+=2)
{
sum2+=a[i];
}
  
check=sum1*3 + sum2;
check=check-1;
check%=10;
check=9-check;
  
if(check==c){
Printf(\"The given barcode is valid\");
  
}
else{
print(\"The given barcode is invalid\")
  
}
}

}

Here we can capture our 12 th digit in a variable and equate it to the check variable and hence validate.

It can be done in other way by increasing the size of array to 12 and capturing the last digit in a[11] and again equate it to checkdigit and validate your array of digits.

2.Here %d and %1d are pretty much similar according to the given scenario both should accept only one digit. In other words \'%1d\' is notifying the scanf to only capture one digit at a time. But using %d we can capture multiple digits. Coming to our question we cannot use %1d in the present scenario because it rules out the possibility of entering -1 or terminating the program because it only accepts a single digit. so, here we have used %d.

I need the CORRECT answers for two questions regarding the code. Heres the link to the code. https://www.chegg.com/homework-help/questions-and-answers/c-program
I need the CORRECT answers for two questions regarding the code. Heres the link to the code. https://www.chegg.com/homework-help/questions-and-answers/c-program

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site