Program is working on code block but not in hacker rank

let’s walk through your code:

  1. malloc(sizeof(int) * num), what is the value of num ?
  2. scanf("%d",num), check the return value of scanf ( number of element successfully parsed by scanf )
  3. for( i = 0 ; i < num; i++ ), loop with i from 0 to num
  4. scanf("%llu",&num_alloc[num]), read number and store it in num_alloc[num] ( indices of array in C start from 0 )
  5. FinalSum = FinalSum + num_alloc[num], add num_alloc[num] to FinalSum ( see #4 )
  6. end loop

I provided some advice to fix your code. But additionally do you need to store each values that are read until the end of the program ?

Also, ‘malloc’ is a function of ‘stdlib.h’ and you didn’t include that header file.

Leave a Comment