-4

I'm trying to get a checksum of a UPC code using the following algorithm:

  • working from right to left, sum the digits in the odd-numbered positions (first, third, fifth, etc.)
  • 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 result should be the check digit.

Here's my code:

int[] a = {1, 2, 3, 4, 5}
checksum = 0;
int i = 0;

while ( a[i] < a.length-1  )
{
  checksum += a[i] + a[i - 1] ;
  i++;
}
checksum = ;

Any ideas?

BryanH
  • 5,826
  • 3
  • 34
  • 47
  • 1
    First you should tell us which language you are using. Second you should properly type checksum. Anyway you might want to lookup wikipedia for "CRC" and/or "MD5". This should give you a start. – Udo Klein Oct 05 '13 at 16:23
  • And your code compiles??? – Udo Klein Oct 05 '13 at 16:25
  • 1
    There are many different checksum algorithms. We'll need the specifics of your assignment to help. – John Kugelman Oct 05 '13 at 16:26
  • 1
    And `I really want to know ASAP please` is not going to help. – Subir Kumar Sao Oct 05 '13 at 16:34
  • I added this same comment down below, but I thought more people would notice it here: Here are the more specific instructions: rray a of ints contains the digits of a number. Complete the following code to store in the variable checksum the checksum of this number calculated using the algorithm described above. – George Isaacson Oct 06 '13 at 00:37
  • Please answer the question: [what have you tried?](http://whathaveyoutried.com) It isn't enough to post a bit of (incomplete) code. Post the expected results and the _actual_ results you get from running your code. – BryanH Oct 07 '13 at 19:10

1 Answers1

0

If you just want to sum all entries of the array, you can do this (not tested):

var sum = 0;
var vars = [1, 2, 3, 4, 5];

vars.forEach(function (callback) {
    sum = sum + callback;
})

alert(sum);

This is not a real checksum, there could also be 5,2,3,1,4

You might also take a look at a similar question on SO. It is about how to to get the md5 checksum of a file; you can also use it for strings.

I can't answer more specifically as your question isn't.

EDIT

If I understood the last line right, this should work (not tested):

var vars = [1, 2, 3, 4, 5];
var sum135;
var sum246;
var i = 0;

while (i < vars.length) {
    sum135 = sum135 + vars[i];
    sum246 = sum246 + vars[i + 1];
    i = i + 2;
}

sum135 = sum135 * 3;
var checksum = (sum135 + sum246) * (sum135 + sum246 + 10) - (sum135 + sum246);
Community
  • 1
  • 1
rudib
  • 227
  • 1
  • 10
  • Here are the more specific instructions: rray a of ints contains the digits of a number. Complete the following code to store in the variable checksum the checksum of this number calculated using the algorithm described above. – George Isaacson Oct 05 '13 at 17:32
  • You mean all the code you posted is given? And you still did't say what checksum you are talking about! – rudib Oct 05 '13 at 17:44
  • Of a UPC code: 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. – George Isaacson Oct 06 '13 at 19:36
  • Seems to be easy i'll post the code tomorrow if i have time p.s. edit your question and maybe try to write the code and post it in the edit... – rudib Oct 06 '13 at 21:11