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.

In the loop, according the given instructions, you can check if the element of the array at the current index is odd or not. If it's odd multiply it by 3 else don't and add it to stepsTotal.

And when adjusting to the closest multiple of 10 >= stepsTotal, simply increment it with 10. We know that if it's equal to stepsTotal, then it's the closest one else the closest will be the 10 added to that number.

In the final step you can subtract stepsTotal from the multiple of 10 so obtained.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.