Why am I getting a segmentation error on this?

I am pretty new to programming, so please ignore the inefficiency of the program.

This is a program to arrange numbers in increasing order, and it runs perfectly in code::blocks, but submitting this as an answer to one of the practice problems gives a segmentation error.

Language is C++.

#include<cstdio>
signed int a[100000];

int main()
{
    int t=0,num=0;
    scanf("%d%",&t);
    for(int i=0;i<100000;++i)
    {
        a[i]=-1;
    }
    for(int j=0;j<t;++j)
    {
        scanf("%d",&num);
        a[num]++;

    }
    for(int k=0;k<100000;++k)
    {
        if(a[k]!=-1)
        {
            for(int l=0;l<=a[k];++l)
            {
                printf("%d\n",k);
            }
        }
    }
    return 0;
}

Each line contains one integer: N [0 <= N <= 10^6]

Just change the size of the array.

1 Like

I did not got any segmentation fault on gcc compiler. Just try to declare the loop variable declarations outside the loop.

Thanks… I did both, changed array size and declared loop variables outside, don’t know which but one worked.

Declaring loop variables inside the loop does not cause runtime error.