[Solved] Program is working on code block but not in hacker rank


A number of things are jumping out at me about your code. You are allocating memory for num integers, when num is uninitialized. It is given an undefined trash value. See undefined behavior. You must put your scanf statement before your malloc, because as it stands right now your code does not make sense.

for ( i=0;i<num;i++)
{
    scanf("%llu",&num_alloc[num]);
    FinalSum =FinalSum + num_alloc[num];
}

For each loop iteration, you are accessing the numth element of your array. This is not possible as num_alloc should be an array of num ints, and array indexes in C start at 0. But what I’m pretty sure what you meant to do is this. With your code, you would have been assigning the same value over and over again.

for (i = 0; i < num; i++) {
    scanf("%llu", &num_alloc[i]);
    FinalSum += num_alloc[i];
}

You must also free() any memory you allocate on the heap with malloc().

free(num_alloc);

1

solved Program is working on code block but not in hacker rank