I'm in the process of writing a Fortran90 program that will take an input file containing points to be graphed, sort those points, calculate the minimum and maximum x and y values, print all of this to another file, and then create a graph and print that as well.
So far, I'm up to calculating the min and max values, and I've got it all printing to a file.
The structure of the program is as such:
Please enter an input file:
GRAPH3.txt
Please enter an output file:
output.txt
And then it'll pull the data from GRAPH3.txt, do stuff, and print it.
GRAPH3.txt looks like this:
11
8.3 8.3
12.0 12.0
2.0 2.0
4.0 4.0
1.0 1.0
4.5 4.5
12.1 12.1
4.6 4.6
3.0 3.0
7.2 7.2
9.0 9.0
The first number indicates how many points there are. The rest are just the pairs of numbers.
When I run my program, this is my output:
presort
8.30 8.30
12.00 12.00
2.00 2.00
4.00 4.00
1.00 1.00
4.50 4.50
12.10 12.10
4.60 4.60
3.00 3.00
7.20 7.20
9.00 9.00
postsort
1.00 1.00
2.00 2.00
3.00 3.00
4.00 4.00
4.00 4.00
4.00 4.00
7.20 7.20
8.00 8.00
9.00 9.00
12.00 12.00
12.00 12.00
Xmin: 1.00
Xmax: 12.00
Ymin: 0.00
ymax: 12.00
Obviously, ymin
should be 1.00
, but it's showing up at 0.00
. Do you have any idea why it would be doing this?
Here's my program:
PROGRAM G6P4
implicit none
character(len=30) :: infile,outfile
logical inexist, outexist,more,quit
integer i,j,n,overwrite
real x(100),y(100),xmax,xmin,ymax,ymin
more=.true.
inexist = .false.
outexist = .false.
overwrite=2
do while (.not.inexist)
print *, 'Please enter an input filename'
read *, infile
INQUIRE(FILE=infile, EXIST=inexist)
if (.not.inexist) then
print *, 'Invalid Input File'
end if
end do
do while (more.or.infile.eq.outfile)
print *, 'Please enter an output filename'
read *, outfile
INQUIRE(FILE=outfile, EXIST=outexist)
if (infile.eq.outfile) then
print *, 'Outfile cannot equal infile.'
else
if (outexist) then
print *, 'File already exists'
print *, '1 to enter overwrite, 2 to enter new file, 0 to quit'
read *,overwrite
select case (overwrite)
case (1)
more =.false.
case (2)
more = .true.
case (0)
more = .false.
quit = .true.
case default
print *,'NOPE'
more = .true.
end select
else
more=.false.
end if
end if
end do
if (quit.eqv..false.) then
OPEN(1, FILE=infile)
OPEN(2, FILE=outfile)
READ(1,*)n
if (n.le.100.and.n.gt.0) then
do i = 1, n
read (1,*) x(i),y(i)
end do
write (2,*) 'presort'
do i = 1, n
write (2,'(2(F6.2,X))') x(i),y(i)
end do
call sort(x,y,n)
write (2,*) 'postsort'
do i = 1, n
write (2,'(2(F6.2,X))') x(i),y(i)
end do
xmin = x(1)
xmax = x(n)
ymax = y(1)
ymax = y(n)
do i = 2, n
if (ymin.GT.y(i)) then
ymin=y(i)
end if
if (ymax.lt.y(i)) then
ymax=y(i)
end if
enddo
write (2,'(A,X,F6.2)') 'Xmin: ',xmin
write (2,'(A,X,F6.2)') 'Xmax: ',xmax
write (2,'(A,X,F6.2)') 'Ymin: ',ymin
write (2,'(A,X,F6.2)') 'ymax: ',ymax
else
print *,'File has invalid number of data points'
end if
end if
end PROGRAM
subroutine sort(x,y,n)
real, intent(out),dimension(100) :: x,y
integer :: n
integer :: end,j,t
logical :: more
end = n-1
do while (more)
more = .false.
do j = 1, end
if (x(j).gt.x(j+1)) then
write (*,'(A,2(F6.1,X))')'Pre: ',x(j),x(j+1)
t = x(j)
x(j)=x(j+1)
x(j+1)=t
t=y(j)
y(j) = y(j+1)
y(j+1)=t
more = .true.
write (*,'(A,2(F6.1,X))')'Post: ',x(j),x(j+1)
end if
enddo
end = end-1
enddo
end subroutine
Another problem I've discovered is that if I remove the write statements from the sort subroutine, for some reason, the program doesn't sort at all. Any clue why it would be doing that?
You are not initializing ymin. You are instead initializing ymax twice. Therefore ymin gets a default initialization of zero, which happens to be the lowest number considered, and is reported as the minimum.