Reputation: 21
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.
Upvotes: 1
Views: 481
Reputation: 29244
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
Upvotes: 1
Reputation: 1835
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
Upvotes: 1