The instructions are long but here goes. The starting code they provided me is after the instructions.
The bar codes used on products in a store are visual encodings of Universal Product Code (UPC) numbers. For example, 036000291452 is a UPC code (in this case for a box of tissues). The rightmost digit of a UPC code is actually a check digit — for our box of tissues the check digit is 2 — that is calculated from the preceding digits — in our case, 03600029145 — as follows:
Working from right to left,
sum the digits in the odd-numbered positions (first, third, fifth, etc.) and multiply the total by three;
sum the digits in the even-numbered positions (second, fourth, sixth, etc.);
add the results of (1) and (2) together, and subtract the total from the closest multiple of ten greater than or equal to that total. The answer is the check digit.
Example: Summing the digits in the odd-numbered positions yields
5 + 1 + 2 + 0 + 6 + 0 = 14;
multiplying by 3 yields 14 × 3 = 42.
Summing the digits in the even-numbered positions yields
4 + 9 + 0 + 0 + 3 = 16.
Adding 42 + 16 = 58, and subtracting 58 from 60 yields the check digit 2.
int[] a = {1, 5, 4, 7 };
int checkdigit;
int stepsTotal = 0;
int pos = 1;
// Loop over array from end to start
for ( int i = a.length - 1; i >= 0; i-- )
{
if ( /* Enter a condition here */ )
stepsTotal += /* Your code here */ ;
else
stepsTotal += /* Your code here */ ;
pos++;
}
// The closest multiple of 10 <= stepsTotal
int multipleOf10 = 10 * ( stepsTotal / 10 );
// If necessary, adjust to the closest multiple of 10 >= stepsTotal
if ( multipleOf10 != stepsTotal )
multipleOf10 += /* Your code here */ ;
checkdigit = /* Your code here */ - stepsTotal;
/Checksum should equal 9 in this example/
Where do I go from here? I have no clue what to do. Also, can someone figure out some pseudocode of some type to get me close to my solution? The code I provide should work for any array length and set of numbers.