17 Array Checksum

Back to Problem Solutions forum

sleepijs     2015-12-25 17:25:06
User avatar

include <stdio.h>

int main(){

long result = 0;
int i,c,l;
long limit = 10000007;
long values [200];
int seed = 113;


printf("Whats the lenght of array?\n");
scanf(" %d", &l);

printf("Enter the values:\n");
for (c = 0; c < l; c++)
scanf("%li", &values[c]);


for(i = 0; i < l; i++){
    result = (result + values[i]) * seed;
    result = result % limit;
}


printf("Answer:\n");
printf("%li", result);

return 0; }

Whats wrong with my code? Example input works perfectly, but test data returns wrong answer. I've tried reading input from a file, initializing array at the time of declaration, changing variable types.. I think its the algorithm, but can't find what's wrong with it. :(

Quandray     2015-12-25 20:22:24
User avatar

We haven't finished with the vowel count problem yet :-)

I can see that you are trying to use long instead of int, but try running

printf("%d %d\n",sizeof(int),sizeof(long));

With modern compilers, a long and an int, both use 4 bytes. Now try

printf("%d %d %d\n",sizeof(int),sizeof(long),sizeof(long long));

You probably want to use long long which is 8 bytes. With scanf and printf try using %lld for long long.

sleepijs     2015-12-26 15:25:35
User avatar

Now it works perfectly. Thanks! :) I'll have to do some reading, I thought changing variable type from int to long would fix it..

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