Comp Sci Solving Fortran 77 Assignments with XYZ: Reading & Calculating Values

AI Thread Summary
The discussion focuses on a user's struggle to read numerical data from a file in Fortran 77 for an assignment involving calculations with an equation. The user shares their initial code, which fails to read values correctly, resulting in an output of zeros. Respondents highlight the need to index the array properly and suggest modifying the READ statement to include the index variable. They also note that Fortran arrays start at index 1, recommending adjustments to the initialization of the index variable. The user expresses gratitude for the assistance and acknowledges ongoing challenges with the programming language.
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:
Physics news on Phys.org
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.
 
  • Like
Likes scottdave
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 FactChecker
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.
 
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.
 
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.
 
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!
 
FYI. These problems are not FORTRAN-specific. The problems you had in this example would have been problems in a lot of languages.
 
  • Like
Likes anorlunda

Similar threads

Replies
3
Views
4K
Replies
5
Views
2K
Replies
7
Views
2K
Replies
1
Views
3K
Replies
2
Views
9K
Replies
1
Views
12K
Replies
10
Views
2K
Replies
1
Views
2K
Back
Top