Utility 5

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,j,n,i1,j1;
float x[10],y[10],min=1000,d[10][10];
clrscr();
printf("Enter n the number of points: ");
scanf("%d",&n);
printf("Enter Coordinate of n points: ");
for(i=0;i<n;i++)
{
scanf("%f%f",&x[i],&y[i]);
}

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
d[i][j]=sqrt((x[j]-x[i])*(x[j]-x[i])+(y[j]-y[i])*(y[j]-y[i]));
}
printf("\nDistance table: \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%0.2f  ",d[i][j]);
}
printf("\n");
}
min=1000;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if ((d[i][j])<min && (j!=i))
{
min=d[i][j];
i1=i;
j1=j;
}
}
}
printf("\nMinimum Distance is %f \n",min);
printf("\nThe Point [%f %f] is nearest to point [%f %f] ",x[i1],y[i1],x[j1],y[j1]);
getch();
}

//Output
Enter n the number of points: 5
Enter Coordinate of n points: 0 2
2 4
4 3
4 1
2 5

Distance table:
0.00  2.83  4.12  4.12  3.61
2.83  0.00  2.24  3.61  1.00
4.12  2.24  0.00  2.00  2.83
4.12  3.61  2.00  0.00  4.47
3.61  1.00  2.83  4.47  0.00

Minimum Distance is 1.000000

The Point [2.000000 4.000000] is nearest to point [2.000000 5.000000]

No comments:

Post a Comment