Getting log of values and writing the value to another matrix

The following code reads the value of Discharge Q and Area A and writes the log of their values into the matrices log_Q and log_A respectively. I can not figure why it only writes the first digit of the log value. When i try to format like ‘(f8.2)’ it only gives 0s. I am writing the code in Fortran 77

      dimension Q(15,1), A(15,1), log_A(15,2), log_Q(15,2),TR(15,15)
      dimension PR(15,15), INV(15,15), H(15,15), SUB(15,15)
      integer X
      print *, "Enter Number of Q values"
      read *, X
      open(1,file="E:\Fortran\Class 5\WECS\Q.txt")
      open(2,file="E:\Fortran\Class 5\WECS\A.txt")
      open(3,file="E:\Fortran\Class 5\WECS\test.txt")
      do 3 i = 1,X
          read(1,*) Q(i,1)
3     continue
      do 4 i = 1,X
          read(2,*) A(i,1)
4     continue

      do 5 i = 1,X
          log_A(i,1) = 1
          log_A(i,2) = log(A(i,1))
5     continue
      do 6 i = 1,X
          log_Q(i,1) = 1
          log_Q(i,2) = log(Q(i,1))
6     continue      
      do 40 i = 1, X
       write (3,*) (log_A(i,j), j = 1,2)

40    continue
      end

The types of log_A and log_Q are not declared, and since you haven’t set implicit none at the beginning of your program, they are determined by the implicit typing rules: if the first letter of the name is i, j, k, l, m, n, then the type is integer, otherwise it is real. Hence log_A and log_Q are considered as integer here, which is not what you want.

The use of implicit none is strongly recommended in all codes.

1 Like

Is it required to use implicit none for fortran 77 as well?
Where should i put that code by the way? It does not work if i put it above dimensioning the arrays or later.

First of all, why would you want to write a code in FORTRAN 77 nowadays? It’s highly unlikely that your compiler be restricted to that version, and if it’s the case you should definitely switch to a modern compiler. Any current compiler is able to compile the above code.

Anyway, the use of implicit none is not required, but recommended. Precisely to avoid issues like the one you are reporting.

implicit none must appear before any variable declaration. If it “does not work”, please show the error messages. Obviously if you just add implicit none to this code, you will have other messages, because then you have to explicitely type all the variables.

      implicit none
      real Q, A, log_A, log_Q, TR, PR, INV, H, SUB
      dimension Q(15,1), A(15,1), log_A(15,2), log_Q(15,2),TR(15,15)
      dimension PR(15,15), INV(15,15), H(15,15), SUB(15,15)
      integer X
1 Like

I’m unsure what you are asking about with this question, but the answer to the literal question is that IMPLICIT NONE was not allowed in f77. That declaration was a common extension in f77 compilers, and it became legal with the f90 revision. The problem with your code is that some of the entities seem to be the wrong type. The way to avoid that is to explicitly declare the types of all of your arrays and variables. You can do that without IMPLICIT NONE, but if you do use IMPLICIT NONE, then the compiler will identify the undeclared entities and it will help you make those declarations, so that is why it is often recommended. It addition to catching errors such as yours, it also catches things like simple typos and misspellings where you create a new variable unintentionally.

Another nice feature of modern fortran (i.e. f90 and later) is free format source code. You are no longer required to begin statements in column 7, to place continuation characters in column 6, and to keep statements within the first 72 columns, and so on. To use this feature, you should probably rename your source code file so that it has a .f90 extension (rather than a .f extension). The details depend a little on your compiler, but that is the de facto convention.

1 Like

You were right!. Fortran 90 is so much easier. Thank you.

Note that the latest Fortran standard version is Fortran 2018, and you can use all features of this latest standard in a .f90 file, without being restricted to Fortran 90 features as some newcomers may think. The reason why free form format is usually denoted by the .f90 extension is that this format had been introduced in Fortran 90. Note also the fixed form format is still part of the standard, but is declared “obsolescent”, so that no new code should be written in this format.

1 Like