vendredi 25 janvier 2019

Why is my code giving an SIGSEGV run time error?

I am trying to implement Sieve of Eratosthenes in this code. This is coded keeping in mind that the range can be 100000 numbers wide with the highest upper limit being 1000000000. Online judge is giving an SIGSEGV runtime error. Please help.

I tried decreasing the array size to 100. It still didn't work. I already switched between local and global variables

int primes[100000]={0}, t, a, b ,i,j,k,l;
int main()
{
    cin>>t;
    for(i=0;i<t;++i)
    {
        cin>>a>>b;
        if(a!=1)
        j=a;
        else
        j=2;
        for(;j<=b;++j) // loop for number count
        for(k=j*j;k<=b;k+=j) // loop for finding composites
        primes[k-1]=1;  //marking out all non primes
        for(l=a;l<=b;++l)
        {
            if(primes[l-1]==0 && l-1!=0)
            cout<<l<<endl;
        }
    }
}

It is working perfectly on my system (I use Dev C++ 5.11) But it won't work with online judges.

Aucun commentaire:

Enregistrer un commentaire