Help!Help! Cant figure out this runtime error

#include <stdio.h>
//Compiler version gcc 6.3.0

 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.

plz send a formatted code
and add problem link

you should declare array after taking its size

Whenever you ask for a doubt, always provide the problem link and the solution link.

Due to this line-

int n,k[n],j,z,e;

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.

1 Like

I did get an TL error.Immediately learned qsort and solved it.Thanks a lot. :slight_smile:

1 Like
//