Solving Fortran 77 Assignments with XYZ: Reading & Calculating Values

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
8 replies · 3K views
Ihatefortran
Messages
4
Reaction score
0
New user's thread moved to the Homework Help forums, so no Template is shown.
Hey guys, new here and was hoping for some help.
Basically I have to use Fortran 77 to complete some questions for an assignment
The first thing to do is, read a bunch of numbers in a list format and then use these numbers to be entered within an equation.
The values I have are listed in a 1 column list eg
4.5252
4.541
5.522
etc

The equation I have is: XYZ (Where Z are the each value from that list, and X/Y are other values). I need to calculate an answer for each Z value.

So far, I'm stuck on even being able to read the file in the first place!
My code:

Fortran:
      PROGRAM Example
      IMPLICIT NONE
      INTEGER n
      REAL phi(10)

      OPEN(10,FILE='data.txt', FORM='FORMATTED',  STATUS='OLD')
      phi=0.0
      n=0
      DO
        READ(10,*,END=10) phi
        n=n+1
      ENDDO
 10   CONTINUE
      PRINT*, phi
      END

When I compile the code just reads 0 for some reason.

Someone help?
 
Last edited by a moderator:
on Phys.org
kuruman said:
It's been a while since I programmed in FORTRAN, but the READ statement looks incorrect. You bump index n and then do nothing with it. I would say
READ(10, * ,END=10) phi(n)
That should read the values into the array.
Did the same thing, just keeps printing 0.00000's
 
Ihatefortran said:
Fortran:
      phi=0.0
      n=0
      DO
        READ(10,*,END=10) phi
        n=n+1
      ENDDO
 10   CONTINUE
      PRINT*, phi
      END
Since this is FORTRAN 77, there shouldn't be a single instance of the variable phi that is not indexed, e.g., phi(n). You need to modify all 3 references to phi in this bit of code.

Also, arrays in FORTRAN start at index 1, so you should initialise n=1.
 
  • Like
Likes   Reactions: FactChecker
DrClaude said:
Since this is FORTRAN 77, there shouldn't be a single instance of the variable phi that is not indexed, e.g., phi(n). You need to modify all 3 references to phi in this bit of code.

Also, arrays in FORTRAN start at index 1, so you should initialise n=1.
FactChecker said:
There are a lot of places where the array phi is not indexed. You should fix them all before proceeding. And move your print inside the loop. And start n at 1.

Thanks guys! Appreciate it.
 
scottdave said:
I came across this late. It looks like you got it figured out? Hopefully you don't hate FORTRAN quite as much. Any new language is frustrating at the beginning, having to learn new syntax and different ways to accomplish a task.
Thanks so much, sort of got the hang of it but still very stuck on another issue now!