Back to Problem Solutions forum
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.
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. :)