Solving Fortran 77 Assignments with XYZ: Reading & Calculating Values

In summary, someone is trying to do an assignment in Fortran and is having some difficulty. They are trying to calculate an answer for each value in a list, but are having trouble reading the file. They are also having trouble with the code compiled.
  • #1
Ihatefortran
4
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
  • #2
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
  • #3
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
 
  • #4
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
  • #5
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.
 
  • #6
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.
 
  • #7
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.
 
  • #8
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!
 
  • #9
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

1. How do I read and calculate values in Fortran 77?

In order to read and calculate values in Fortran 77, you will need to use the READ statement to input the values and the WRITE statement to output the calculated results. You will also need to declare the necessary variables and use appropriate mathematical operators.

2. What is the role of XYZ in solving Fortran 77 assignments?

XYZ is a placeholder for the specific variables and values that are used in the Fortran 77 program. It can represent any type of data, such as numbers, characters, or logical values, and can be used in calculations and data manipulation.

3. Are there any specific rules for solving Fortran 77 assignments?

Yes, there are certain rules and guidelines that must be followed in order to successfully solve Fortran 77 assignments. These include properly declaring and using variables, following the correct syntax for statements, and understanding the order of operations for mathematical calculations.

4. Can Fortran 77 be used for more complex calculations?

Yes, Fortran 77 can handle more complex calculations by using control structures such as DO loops and IF statements. These allow for repetition and conditional execution of statements, making it possible to solve more complex problems.

5. Is there a limit to the number of values that can be read and calculated in a Fortran 77 program?

The maximum number of values that can be read and calculated in a Fortran 77 program depends on the size of the computer's memory. However, it is recommended to break down larger calculations into smaller parts to avoid memory issues and improve program efficiency.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
3K
  • Programming and Computer Science
Replies
4
Views
619
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
Back
Top