You are code gives segmentation fault because you are accessing memory after array index size.
To debug it I simply added two likes in you code to print n, i , j
:
if(b[i]<a[i])
{
for(j=n;j>=i;j--)
{
a[j+1]=a[j];
}
a[i]=b[i];
n = n+1; "<---- 1 Correction need here"
printf("In IF %d %d %d\n", i, j, n);
}
else
{
for(j=n;j>=i;j--)
{
a[j+1]=a[j];
}
a[i+1]=b[i];
n++; "<----2 Correction need here"
printf("In Else %d %d %d\n", i, j, n);
}
its output is:
$ ./a.out
In IF 0 -1 4
In Else 1 0 5
In Else 2 1 6
In Else 3 2 7
In Else 4 3 8
In IF 5 4 9
In IF 6 5 10 <-- i, j, n "buffer overflow"
In IF 7 6 11 <-- i, j, n
In IF 8 7 12 <-- i, j, n
In IF 20 11 13 <-- i, j, n
3 4 5 10 13 5 5 10
Suggestion don’t change n
in your loop change you logic on this
1
solved missing terminating > character at line 17 [closed]