Question

I am trying to print a graph in Fortran77 by creating a 2d CHARACTER array. The x value is constant (number of lines in my file) and my y value will be inputted by the user. I initialized all the values to be spaces but I am unsure as to how to fill the 2d array with the points in my file (eg - 1 and 100, 2 and 200) as well as how to prompt the user for a height. Any ideas?

Code:

      SUBROUTINE PLOT(L,S)
      INTEGER*8 L
C S = the variable that the user gives for y length, user defined (not sure how to do yet)
      CHARACTER H(L,S)

C
C LOCAL VARIABLES
C
      INTEGER I

      DO 100 I=1, S
        H(I,I) = ' '
100 CONTINUE

      RETURN
      END

My data file:

1     100
2     200
3     300
4     400
5     500
Was it helpful?

Solution

Here are some basic read and write:

To read in a file called data_in with N lines and variables x, y, z in each line:

open(unit=10,file='data_in',status='old')
do i=1,N
  read(10,*) x(i), y(i), z(i)
end do
close(10)

To write variables x, y, z of dimension N each to a file called data_out:

open(unit=10,file='data_out',status='unknown')
do i=1,N
  write(10,*) x(i), y(i), z(i)
end do
close(10)

To take x, y and z as input from the keyboard (standard input, *) asking the user before hand:

do i=1,N
  write(*,*) 'Please input element number ', i, ' of variables x, y and z, respectively'
  read(*,*) x(i), y(i), z(i)
end do

In each of the three cases, the first option in read and write is the input/output unit, either referred to with a number (I have used 10 after opening the files with that identifier) of with an asterisk *, which signifies "standard input/output", the terminal prompt in this case. The second option, for which I have used an asterisk * in all cases refers to the format. * is the format "as given".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top