Solving Fortran 77 Assignments with XYZ: Reading & Calculating Values

Click For Summary

Discussion Overview

The discussion revolves around using Fortran 77 to read numerical data from a file and perform calculations based on that data. Participants are addressing specific coding issues related to reading values into an array and the correct syntax for Fortran 77 programming.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant expresses difficulty in reading values from a file into an array in Fortran 77 and shares their initial code.
  • Another participant suggests modifying the READ statement to include the index for the array, indicating that the current implementation does not store values correctly.
  • Several participants emphasize the need to index the array phi in all references and point out that Fortran arrays start at index 1, recommending initializing n to 1.
  • There are repeated suggestions to move the print statement inside the loop to display values as they are read.
  • Some participants express empathy towards the original poster's struggles with learning Fortran, noting that similar issues could arise in other programming languages.

Areas of Agreement / Disagreement

Participants generally agree on the need to index the array correctly and modify the code accordingly. However, there is no consensus on the effectiveness of the proposed solutions, as some participants continue to experience issues with the code.

Contextual Notes

Limitations include the potential for misunderstanding the indexing rules in Fortran 77 and the specific syntax required for reading data into arrays. The discussion does not resolve all coding issues, and participants express ongoing challenges.

Who May Find This Useful

Individuals learning Fortran 77, those working on programming assignments involving file I/O, and participants interested in coding practices for numerical data handling may find this discussion relevant.

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   Reactions: 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   Reactions: 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   Reactions: anorlunda

Similar threads

  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
9K
  • · Replies 1 ·
Replies
1
Views
12K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K