int main()
{
int n,k[n],j,z,e;
scanf ("%d",&n);j=0;z=0;
for (j=0;j <n;j++)
{
scanf("%d",&k [j]);
}
for (z=0;z <n;z++)
{
for (j=z+1;j <n;j++)
{
if (k[z]>k [j])
{
e=k [z];k [z]=k [j];k [j]=e;
}
}
}
for (j=0;j <n;j++)
printf ("%d\n",k [j]);
return 0;
}
Im getting a runtime error SIGSEGV for this.The code runs fine though.how do i fix this?Please help.
We ahve not yet inputted value of n, so its assigned soem random garbage value. But we are simulaneously declaring an array of size n, which is wrong. Declare your array in one of later lines, after you input n, and you will get past SIGSEV.
But, your approach will get TLE if i am not wrong. Try learning about in built sort functions in c and c++, and see accordingly.