Skip to main content
  1. About
  2. For Teams
Asked
Viewed 507 times
1

I have been trying to run the code which arrange the array in decreasing order. The array is takem from the input file and then arranged goed into the output file. The code compiles fine, but when i run a.exe it shows an error. Here is the code:

    program arrangingARRAY
     dimension a(100)

     open(1,file="array.txt")
     read(1,*) n

     do i =1, n
         read(1,*) a(i)
     enddo
     close(1)

     i=1
1    continue
     j=i+1

2    continue
     if (a(j).lt.a(i)) then
        goto 3
     else
     s=a(i)
     a(i)=a(j)
     a(j)=s
     end i
3    continue
     j=j+1
     if (j.le.n) then
         goto 2
     else
         i=i+1
     end if
     if (i.lt.n) then
         goto 1
     end if
     open(1,file="decreasingARRAY.txt")

     do i=1,n
          write(1,*) a(i)
     enddo
     end

the input file looks like this:

60
123
60
21
1
2
3
4
11
90

I have tried to put the empty line into the txt file, but it doesn`t work. If you have any solutions to this problem, please help.

1
  • You read the first number with read(1,*) n and then proceed to read n numbers from the file. Does the input file have 60 entries?
    John Alexiou
    –  John Alexiou
    2021-09-21 04:37:58 +00:00
    Commented Sep 21, 2021 at 4:37

2 Answers 2

1

Your code does not even compile. But I assume the line end i is a typo. But more importantly, you are reading the number of lines n from the input file and the input file does not contain the number of lines, or if it does, then it is wrong (60). There are 10 lines in the code. So either add 10 to the first line of your code, or fix n = 10 in your code to resolve the problem. Also, avoid numbered lines and goto statements. such syntax belongs to a half-century ago, to the era of punch cards. Use implicit none at the beginning of every program unit. The following fixes the problem and some of the syntactical issues of your code.

program arrangingARRAY

    implicit none

    real :: Array(100), s
    integer :: fileUnit, numLine, i, j

    open(newunit = fileUnit,file = "array.txt")

    !read(fileUnit,*) numLine
    numLine = 10
    do i =1, numLine
        read(fileUnit,*) Array(i)
    enddo
    close(fileUnit)

    i=1
1   continue
    j = i + 1
2   continue

    if (Array(j) >= Array(i)) then
        s = Array(i)
        Array(i) = Array(j)
        Array(j) = s
    end if

    j = j + 1
    if (j <= numLine) then
        goto 2
    else
        i = i + 1
    end if
    if (i < numLine) then
        goto 1
    end if

    open(1,file="decreasingARRAY.txt")
    do i=1,numLine
        write(1,*) Array(i)
    enddo

end

Here is the output (for the same input as yours),

   123.0000    
   90.00000    
   60.00000    
   60.00000    
   21.00000    
   11.00000    
   4.000000    
   3.000000    
   2.000000    
   1.000000    
Sign up to request clarification or add additional context in comments.

Comments

1

I think the data are of integer type and you have to read until the end of the file to determine how many they are. Use the method described here for this. In your code, you are using the first number to decide how many numbers to read, and it looks like this is incorrect.

Also, it looks you are writing Fortran 77 code, and since you are learning you might as well use modern Fortan with do/end do loops

Try this:

program SortSO
use, intrinsic :: iso_fortran_env, only : iostat_end
implicit none

integer :: i, j, n, lu, ierr
integer :: a(100), t
i = 0
! Read values until end of file
open(newunit = lu, file="array.txt")    
do
    i = i + 1
    read (lu, *, iostat=ierr) a(i)
    if( ierr == iostat_end ) then
        n = i - 1
        exit
    end if
end do    
close(lu)

! Sort values
do i=1,n-1
    do j=i+1,n
        if( a(i)<a(j) ) then
            t = a(j)
            a(j) = a(i)
            a(i) = t
        end if
    end do
end do

! Export values
open(newunit = lu, file="output.txt")    
do i=1,n
    write (lu,*) a(i)
end do
close(lu)


end program SortSO

with output.txt containing

     123
      90
      60
      60
      21
      11
       4
       3
       2
       1

3 Comments

eof() is not standard Fortran, and so should not be used. In a bit I'll dig out the proper way to do this.
See stackoverflow.com/questions/54399425/… to find the correct way to detect an end of File in Fortran - it covers not just F77 but modern Fortran as well
@IanBush - thank you, I edited the post with the iostat method.

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.