17 Array Checksum - Is my checksum algorithm correct?

Back to Problem Solutions forum

ugsdhfjbnsja     2015-07-21 20:19:26

Written in C:

This task seems very simple, however I keep getting the incorrect answer. Thsi extract of code contains the actual algorithm for calculating the checksum in my program. I have gone over and over it and cannot see any issue with its logic. I even recently added in the first mod operation after the addition to guard against overflows but it still gives incorrect answers. I am using unsigned long ints in all my results variables so enable the variables to hold integer values beyond the scope of the program specifications.

result = input[0];
// Sorry, I've erased the rest of code to prevent spoiling solution - Admin :)

If this is correct, then I am simply making a stupid mistake elsewhere.

Rodion (admin)     2015-07-21 20:45:58
User avatar

Hi, Mike!

Thanks for your message. No, this is not a "stupid" mistake. It is quite non-obvious - the matter is in initial conditions. The article accompanying the tasks says you should initialize result to zero and process all elements. You instead initialize result with the first element and process the others.

The difference is that the first element is then never multiplied by seed (you really start with the sum of two first).

To be honest you've puzzled me - for at first glance I thought I myself will wrote the code just like you did. :)

ugsdhfjbnsja     2015-07-21 21:05:33

Oh dammit! That's so simple. How did I overlook that?

Thanks for pointing it out.

Please login and solve 5 problems to be able to post at forum