/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
/* Online C++ Compiler and Editor */
#include<stdio.h>
#include<math.h>
struct point
{
float x,y;
};
void sort(float *d,struct point *p);
int main()
{
int n;
printf("Enter the number of points in the system:");
scanf("%d",&n);
float d[n],xi,yi;
struct point p[n];
printf("\nEnter the X and Y coordinates of the points-\n");
for(int i=0;i<n;i++)
{
printf("Enter the X coordinate of point %d: ",i+1);
scanf("%f",&p[i].x);
printf("Enter the Y coordinate of point %d: ",i+1);
scanf("%f",&p[i].y);
}
printf("\nPoints entered are-\n");
for(int i=0;i<n;i++)
{
printf("%d- (%0.2f,%0.2f)\n",i+1,p[i].x,p[i].y);
}
printf("\nEnter the X coordinate of point from which distance is to be calculated:");
scanf("%f",&xi);
printf("Enter the Y coordinate of point from which distance is to be calculated:");
scanf("%f",&yi);
for(int i=0;i<n;i++)
{
d[i]=fabs(sqrt(pow(((p[i].x)-xi),2)+pow(((p[i].y)-yi),2)));
}
printf("\nDistance- ");
for(int i=0;i<n;i++)
{
printf("%0.2f ",d[i]);
}
printf("\n");
sort(d, p);
printf("\n\nSorted distance- ");
for(int i=0;i<n;i++)
{
printf("%0.2f ",d[i]);
}
printf("\n\n");
for(int i=0;i<n;i++)
{
printf("%d- (%0.2f,%0.2f)\n",i+1,p[i].x,p[i].y);
}
}
void sort(float *d,struct point *p)
{
for(int i=0;i<sizeof(d)/sizeof(float);i++)
{
for(int j=1;j<(sizeof(d)/sizeof(float))-i;j++)
{
if(*(d+i) > *(d+i+j))
{
float temp=*(d+i),pxtemp=(p+i)->x,pytemp=(p+i)->y;
*(d+i) = *(d+i+j);
*(d+i+j)=temp;
(p+i)->x = (p+i+j)->x;
(p+i+j)->x=pxtemp;
(p+i)->y = (p+i+j)->y;
(p+i+j)->y=pytemp;
}
}
}
}