CSCI1120 Assignment 1: Check Digit Computation solution

$30.00

Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment

Description

5/5 - (7 votes)

Introduction
Every credit card number contains a check digit at its rightmost, which is used for simple error
detection. It can be used to protect against accidental errors such as a mistyped digit or the
permutation of two successive digits. In this assignment, you will write a program that computes the
check digit of a partial credit card number. For simplicity, we assume that a card number has exactly
16 digits in the form 𝑑1𝑑2𝑑3𝑑4𝑑5𝑑6𝑑7𝑑8𝑑9𝑑10𝑑11𝑑12𝑑13𝑑14𝑑15𝑑16, in which 𝑑1 … 𝑑15 is the 15-
digit partial card number obtained from the program user and 𝑑16 is the check digit to be computed.
To compute the check digit of a partial card number, we can use the Luhn algorithma described
below:
1. Double the odd-positioned digits 𝑑1, 𝑑3, 𝑑5, 𝑑7, 𝑑9
, 𝑑11, 𝑑13, and 𝑑15.
2. If a doubled digit is greater than 9, replace it by its sum of digits. (E.g., 16 is replaced by 1 + 6
= 7.)
3. Sum the even-positioned digits 𝑑2, 𝑑4, 𝑑6, 𝑑8, 𝑑10, 𝑑12, and 𝑑14 with the modified oddpositioned digits 𝑑1, 𝑑3, 𝑑5, 𝑑7, 𝑑9
, 𝑑11, 𝑑13, and 𝑑15.
4. Multiply the sum by 9. Then the units digit (個位數, the rightmost digit) of the multiplication
is the check digit 𝑑16.
Example 1: partial card number is 763545841927506.
1. Double the underlined odd-positioned digits in 763545841927506: (7Μ² Γ— 2) = 14, (3 Γ— 2) =
6, (4 Γ— 2) = 8, (8Μ² Γ— 2) = 16, (1Μ² Γ— 2) = 2, (2Μ² Γ— 2) = 4, (5Μ² Γ— 2) = 10, and (6Μ² Γ— 2) = 12.
2. Replace 14, 16, 10 and 12 by their sum of digits: 14 οƒ  1 + 4 = 5, 16 οƒ  1 + 6 = 7, 10 οƒ  1 + 0 =
1, and 12 οƒ  1 + 2 = 3.
3. Sum all 15 digits: 5 + 6 + 6 + 5 + 8 + 5 + 7 + 4 + 2 + 9 + 4 + 7 + 1 + 0 + 3 = 72. (The
digits in red came from steps 1 and 2.)
4. Multiply the sum 72 by 9: 72 Γ— 9 = 648Μ². The units digit (8) is the check digit. Therefore, the
full card number is: 7635458419275068.
Example 2: partial card number is 543210987654321.
1. Double the underlined digits in 543210987654321: (5Μ² Γ— 2) = 10, (3 Γ— 2) = 6, (1 Γ— 2) = 2,
(9 Γ— 2) = 18, (7Μ² Γ— 2) = 14, (5Μ² Γ— 2) = 10, (3Μ² Γ— 2) = 6, and (1Μ² Γ— 2) = 2.
2. Replace 10, 18, 14 and 10 by their sum of digits: 10 οƒ  1 + 0 = 1, 18 οƒ  1 + 8 = 9, 14 οƒ  1 + 4 =
5, and 10 οƒ  1 + 0 = 1.
3. Sum all 15 digits: 1 + 4 + 6 + 2 + 2 + 0 + 9 + 8 + 5 + 6 + 1 + 4 + 6 + 2 + 2 = 58. (The
digits in red came from steps 1 and 2.)
4. Multiply the sum 58 by 9: 58 Γ— 9 = 522Μ². The units digit (2) is the check digit. Therefore, the
full card number is: 5432109876543212.

a Reference: https://en.wikipedia.org/wiki/Luhn_algorithm
Program Specification
The program should repeatedly accept a user input until a negative integer is entered. You can
assume that the user input is always either a 15-digit number or a negative integer. (That is, you do
not have to check whether the input is out of this assumption.) If the input is a 15-digit number, then
you have to use the Luhn algorithm to compute the check digit and display the full card number. The
full card number should be printed as four pieces of 4-digit segments in the format XXXX-XXXX-XXXXXXXX. If the input is a negative integer, then the program terminates.
Note that the int data type is not big enough to represent 15-digit numbers. In order to store the
partial or full card numbers, you can declare variables of the long long type, which is a bigger
integral data type.
long long num; // int num; does not work
You are not allowed to use arrays in this assignment.
Program Output
The following shows some sample output of the program. The bold blue text is user input and the
other text is the program output. You can try the provided sample program for other input. Your
program output should be exactly the same as the sample program (i.e., same text, same symbols,
same letter case, same number of spaces, etc.). Otherwise, it will be considered as wrong, even if
you have computed the correct result.
Enter a 15-digit partial card num: 763545841927506↡
Full card num is: 7635-4584-1927-5068
Enter a 15-digit partial card num: 543210987654321↡
Full card num is: 5432-1098-7654-3212
Enter a 15-digit partial card num: 432100080012056↡
Full card num is: 4321-0008-0012-0564
Enter a 15-digit partial card num: -1↡
Bye!
Submission and Marking
ο‚· Your program file name should be luhn.cpp. Submit the file in Blackboard
(https://elearn.cuhk.edu.hk/).
ο‚· Insert your name, student ID, and e-mail address as comments at the beginning of your source
file.
ο‚· You can submit your assignment multiple times. Only the latest submission counts.
ο‚· Your program should be free of compilation errors and warnings.
ο‚· Your program should include suitable comments as documentation.
ο‚· Plagiarism is strictly monitored and heavily punished if proven. Lending your work to others is
subjected to the same penalty as the copier.