0 Replies - 457 Views - Last Post: 30 September 2012 - 08:57 AM Rate Topic: -----

#1 Dontshootimpreggo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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"
READ *, k, x1,y1, x2,y2,,,,xk,yk,xn,yn
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)
        READ *, xk,yk,xn,yn
        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