Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran77 Help Please

  1. Dec 20, 2018 #1
    • 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:

    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: Dec 20, 2018
  2. jcsd
  3. Dec 20, 2018 #2

    kuruman

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    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.
     
  4. Dec 21, 2018 #3
    Did the same thing, just keeps printing 0.00000's
     
  5. Dec 21, 2018 #4

    DrClaude

    User Avatar

    Staff: Mentor

    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.
     
  6. Dec 21, 2018 #5

    FactChecker

    User Avatar
    Science Advisor
    Gold Member
    2018 Award

    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.
     
  7. Dec 23, 2018 #6
    Thanks guys! Appreciate it.
     
  8. Dec 24, 2018 #7

    scottdave

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    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.
     
  9. Jan 1, 2019 #8
    Thanks so much, sort of got the hang of it but still very stuck on another issue now!
     
  10. Jan 1, 2019 #9

    FactChecker

    User Avatar
    Science Advisor
    Gold Member
    2018 Award

    FYI. These problems are not FORTRAN-specific. The problems you had in this example would have been problems in a lot of languages.
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?