0

Recently I came across this question and I'm able to write a code for this which works fine in all online compilers but showing segmentation fault in the problem page!! Can somebody help me to eliminate this error? The problem page is also linked which has the question and compiler where I'm getting a segmentation fault.

#include <stdio.h>

int main() 
{
    int num=0;
    scanf("%d",&num);
    
    long long int arr[num], i, j, k, m;
    for(i=0; i<num; i++)
        scanf("%lld",&arr[i]);
    
    for(k=0; k<num; k++)
    {
        long long int val = arr[k];
        long long int tmp[val], count=0;
        
        for(m=1; m<=arr[k]; m++)
        {
            tmp[m] = 0;
        }
        
        for(i=1; i<=arr[k]; i++)
        {
            for(j=i; j<=arr[k]; j+=i)
            {
                if(tmp[j] == 0)       { tmp[j] = 1; count++;}
                else if(tmp[j] == 1)  { tmp[j] = 0; count--;}
            }
        }
        printf("%lld\n",count);
    }
    return 0;
}

Link to problem page

Ôrel
  • 7,044
  • 3
  • 27
  • 46
  • 1
    The `val` must be be a constant value. Try using pointers and allocating memory dynamically. – Rohan Bari Jun 24 '20 at 19:47
  • I don’t think declaring `long long int arr[num];` works the way you think - try `long long int *arr = malloc(num*sizeof( long long int));` Similarly revise `long long int tmp[val];` to use `malloc`. An array declaration only works with a constant, not a runtime value. – DisappointedByUnaccountableMod Jun 24 '20 at 19:49
  • 2
    `for(m=1; m<=arr[k]; m++) { tmp[m] = 0; }` - arrays are 0-based. You are off-by-one here – Eugene Sh. Jun 24 '20 at 19:49
  • 1
    Turn on all your compiler warnings (and leave them on) it will probably tell you that your code is not using a const to set the size of an array - and learn to always respect/never ignore compiler warnings – DisappointedByUnaccountableMod Jun 24 '20 at 19:54
  • 1
    @barny C allows variable-length arrays. – Barmar Jun 24 '20 at 19:55
  • 1
    @RohanBari No. The array size doesn't have to be constant in C99. – Lxer Lx Jun 24 '20 at 20:09
  • @Barmar Yes, nowadays' C allows variable-length arrays. But only variable-length arrays **of reasonable lenght**. Are you aware of any real-life execution environment that will let you to allocate **8 trillions** bytes on the stack? Note the problem statement defined maximum possible N as 10^12, multiply it by 8 (assuming `long long int` being 64-bit) and you need 8*10^12 bytes, i.e. 8 TB... – CiaPan Jun 24 '20 at 20:10
  • @CiaPan I hadn't read the problem statement at the link, didn't realize `n` could be so large. – Barmar Jun 24 '20 at 20:13
  • I suppose two initial paragraphs of [my answer](https://stackoverflow.com/a/31274799/733637) at the question [_Allocating array of size 10^5 * 10^5 in c using malloc_](https://stackoverflow.com/questions/23584664/allocating-array-of-size-105-105-in-c-using-malloc) apply here, too. – CiaPan Jun 24 '20 at 20:18
  • One lesson for this type of submission is that you should always test your code to the full range of constraints given. – Weather Vane Jun 24 '20 at 20:26

0 Answers0