Array Checksum

Back to General discussions forum

radhitya     2024-07-17 04:01:16

This is my code:

    #include <stdio.h>
    int calculate_checksum(int length, int array[]) {
    int result = 0;
    int seed = 113;
    int limit = 10000007;
    for(int i = 0; i < length; i++) {
            result = (result + array[i]) * seed;
            result = result % limit;
                        }
    return result;
    };
    int main() {
    int x,i;
    scanf("%d", &x);
            int jawaban[x];

    for(i=0;i<x;i++) {
    scanf("%d", &jawaban[i]);
    }

    int proses = calculate_checksum(x,jawaban);

    printf("%d ", proses);
    return 0;
    }

When i put the example, the code generates same answer.

But is not for test data.

Where are my code fault?

gardengnome     2024-07-17 05:17:17
User avatar

Hi, you store everything as int. Have a look whether your calculations could produce numbers greater than the max value for int in C++, i.e. whether you could get an overflow error.

radhitya     2024-07-19 03:11:48

okay thank you @gardengnome

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