
Robert C. answered 10/09/21
Tutor
4.8
(6)
Senior iOS/Swift Developer, Lecturer, Computer Science Graduate
Hi Diana, I'm offering some help by associating some of the code you'll need with comments based on the instructions. I hope this helps. Let me know if you have any further questions.
uint64_t shift_digits(uint64_t digits) {
// You can remove the last digit by dividing it by 10
return digits / 10;
}
uint64_t last_digit(uint64_t digits) {
// You can retrieve the last digit using modulo 10
return digits % 10;
}
void process_upc(uint64_t upc) {
int i;
// The check digit is the last digit, you’ll want to save that value first
uint64_t check = last_digit(upc);
uint64_t digits = shift_digits(upc);
uint64_t odd_sum = 0;
uint64_t even_sum = 0;
uint64_t result; // TODO: compute result
uint64_t computed_check;
// There are exactly 11 remaining digits after that (which calls for a for loop)
for (i = 0; i < 11; i++) {
uint64_t last = last_digit(digits);
// Even number indexes represent odd-numbered positions
if (i % 2 == 0) {
// TODO: Add the digits in the odd-numbered positions
} else {
// TODO: Add the digits in the even-numbered positions
}
digits = shift_digits(digits);
}
// TODO: multiply sum of odd-numbered positions by three
// TODO: Add the digits in the even-numbered positions to the result.
// TODO: Take the remainder of the result divided by 10 (ie. the modulo 10 operation).
//TODO: If the remainder is equal to 0 then use 0 as the check digit, and if not 0 subtract the remainder from 10 to derive the check digit.
if (result % 10 == 0) {
computed_check = ??;
} else {
computed_check = ??;
}
}