mercredi 19 octobre 2016

Why does this code have a segmentation fault?

I find that that while running this code it says: Thread1:EXC_BAD_ACCESS(code=1,address=0x7fff3010efcc)

Code:

#include <iostream>
using namespace std;
int main()
{
int d[20],n,k,j,i,a[100000000],count=0;
//long long int i,a[100000000];
cin>>n>>k;
for(i=0;i<k;i++)
{
    cin>>d[i];
}
for(i=0;i<n;i++)
{
    a[i]=i;
}
for(i=0;i<n;i++)
{
    for(j=0;j<k;j++)
    {
        if(a[i]%d[j]==0)
        {
            a[i]=0;
        }
    }
}
for(i=0;i<n;i++)
{
    if(a[i]!=0)
    {
        count++;
    }
}
cout<<count;

}

Aucun commentaire:

Enregistrer un commentaire