#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define gc getchar_unlocked
#define distance_formula sqrt((pow((y2-y1),2)+pow((x2-x1),2)));
int compare(void const ,void const );
static int get_i()
{
char c = gc();
while(c<‘0’ || c>‘9’)
c=gc();
int ret=0;
while(c>=‘0’ && c<=‘9’)
{
ret = ret * 10 + c-‘0’;
c=gc();
}
return ret;
}
int main(int argc, char const argv[])
{
int t=get_i();
printf("\n");
while(t–)
{
int n=get_i();
int i;
int a;
a=(int)malloc(n * sizeof(int));
for(i=0;i<n;i++)
{
a[i]=(int)malloc(2sizeof(int));
a[i][0]=get_i();
a[i][1]=get_i();
}
qsort(a,n,sizeof(int),compare);
/* then the algorithm of the question */
printf("%.2lf\n",distance_formula());
}
return 0;
}
int compare(void const *p,void const *q)
{
const int *x = *(const int **)p;
const int *y = *(const int **)q;
if(x[0]==y[0])
return x[1]-y[1];
else
return x[0]-y[0];
}
What I am trying to do here is that if I have got an array I am first trying to sort it keeping the order intact. But this snippet is showing segmentation fault error. Can someone please help me with my problem?
If anyone can suggest me the algorithm I would really appreciate.
Also I would like to have some suggestions on removing segmentation fault because whenever I try to use dynamic concept, it invariantly throws a Segmentation Fault where I don’t want to bypass the error check using -fno-stack-protector
-regards