Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sorting numbers in increasing order...

Status
Not open for further replies.

kostalibe

Technical User
Jun 16, 2008
5
GR
Hi,
I have this code for sorting N numbers X(I) in increasing order.

DIMENSION X(100)
READ (*,*) N, (X(I),I=1,N)
N1=N-1
DO 15 I=1,N1
AMIN=X(I)
M=I+1
DO 25 K=M,N
IF (AMIN.LE.X(K)) GO TO 25
Z=AMIN
AMIN=X(K)
X(K)=Z
25 CONTINUE
15 X(I)=AMIN
WRITE (*,2) (X(I),I=1,N)
2 FORMAT (10(3X,F5.2))
STOP
END

I dont want to read data from keyboard but from a file.And then write the result in to a another file.
Does anyone know how this will work??

Many thanks
 
Change
Code:
    READ (*,*) N, (X(I),I=1,N)
...
     WRITE (*,2) (X(I),I=1,N)
to
Code:
      OPEN (10, file='input.txt', status='old')
      READ (10, *) N, (X(I), I = 1, N)
      CLOSE (10)
...
      OPEN (20, file='output.txt', status='new')
      WRITE (20, 2) (X(I), I = 1, N)
      CLOSE(20)
10 and 20 are just arbitrary numbers - can be anything you like. Some people like low single digit ones like 1 and 2, some like them in their 10s others in their 100s or 1000s. You can also have them as PARAMETER constants which aren't so error prone.
 
thanks a lot, but i need a little more help.
My file,which i want to read and put the data in increasing order, has many many rows(i dont know how many!) and 6 columns.
I only need to read the first column. Then when I execute my program...

program ORDER

DIMENSION X(100)
OPEN (10, file='myfile.DAT', status='old')
OPEN (20, file='output.dat', status='new')
READ (10, *) N, (X(I), I = 1, N)
N1=N-1
DO 15 I=1,N1
AMIN=X(I)
M=I+1
DO 25 K=M,N
IF (AMIN.LE.X(K)) GO TO 25
Z=AMIN
AMIN=X(K)
X(K)=Z
25 CONTINUE
15 X(I)=AMIN
WRITE (20,*) (X(I), I = 1, N)
CLOSE (10)
CLOSE(20)
STOP
END

it shows: user breakpoint code... and says: list-direct I/O syntax error, unit 10, etc....

what is the problem??
thanks a lot again for your kind help
 
1) You are reading N first - is this the number of entries in the file?
2) READ (10, *) will read in free format: not one per line. If you want one per line, do not use an implied do loop: use a normal do loop i.e.
Code:
DO I = 1, N
   READ(10,*) X(I)
ENDDO
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top