# Fortran Problem(Closest points in set of points)

Page 1 of 1

## 0 Replies - 473 Views - Last Post: 30 September 2012 - 08:57 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=293717&amp;s=65f7df9ee736c67f260ad6a5521845e8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Dontshootimpreggo

Reputation: 0
• Posts: 1
• Joined: 30-September 12

# Fortran Problem(Closest points in set of points)

Posted 30 September 2012 - 08:57 AM

Just learning to write in fortran now as I'm fairly new to programming all around, found a practice problem to write a program which will find a pair of closest points in a set of two-dimensional points (x1,y1),(x2,y2),...,(xk,yk)

I assumed that points (x1,y1) and (x2,y2) where the closest for the initial conditions then I'm trying to set up the algorithm to systematically check the distance of all the other pairs of point and then switch those with (x1,y1) and (x2,y2) if they were found to be closer.

```!Let a and b be the two closest sets of points.

PROGRAM Coordinates
IMPLICIT NONE
REAL :: x1,y1,x2,y2,xk,yk,dist,xn,yn
INTEGER :: a,b,n,k
PRINT *, “Enter the number of points and the coordinates of these points"
IF(k<=2 .OR. k=>1000)THEN
PRINT *, “The number of points to be considered must be between 2 and 1000)
ELSE
a=(x1,y1)
b=(x2,y2)
k=1
DO WHILE(k=>2 .OR. k<=1000)
IF(sqrt((x2-x1)**2+(y2-y1)**2))<=sqrt((xk-xn)**2-(yk-yn)**2))THEN
PRINT *, “The points (x1,y1) and (x2,y2) are the two closest points”
ELSE IF(sqrt((xk-xn)**2-(yk-yn)**2)<=sqrt(x2-x1)**2+(y2-y1)**2))THEN
a=(xk,yk)
b=(xn,yn)
k=k+1
n=n+1
ENDDO
ENDIF
PRINT *, “the two closest points are”, a, “and”, b
ENDPROGRAM Coordinates
```

The issue is that i'm unsure whether the (xk,yk) and (xn,yn) setup will work where x and k are two arbitrary values such that the algorithim moves on and reads the next two points until it has exhausted all possible combinations of points, is there a better way to write this if this doesn't work?

Is This A Good Question/Topic? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }