0

Suppose I have the matrix c(i,j). I want to write it on the screen on oldest Fortran77 language with three signs after comma. I write

         do i=1,N
         write(*,"(F8.3)") ( c(i,j), j=1,N )
         end do

but the output is in the form

c(1,1)

c(1,2)

...

c(1,10) c(2,1)

c(2,2)

...

Finally, I may simply write

         do i=1,N
         write(*,*) ( c(i,j), j=1,N )
         end do

and then the output is like the matrix, but, of course, it is not formatted.

How to get the correct output in Fortran77?

An edit. It seems that one of solutions is to write

   do i=1, N
   do j=1, N
    write(*,'(F9.3,A,$)') c(i,j), ' '
   end do
    write(*,*) ' '
   end do
John Taylor
  • 137
  • 1
  • 9
  • 1
    The `$` you have added here is technically nonstandard I believe and can be replaced with, `advance='no'` in more modern fortran (e.g. `write(*,'(F9.3,A)',advance='no') c(i,j), " "`). – d_1999 Jun 07 '16 at 15:02
  • 1
    Note, that your solution is NOT Fortran 77. The question is, why are you asking for Fortran 77? You have to dig deep to find F77 only compilers still around. Your code snippets definitely are not F77, thus it is not quite clear what you are asking for. – haraldkl Jun 07 '16 at 15:22
  • @haraldkl : this is not true, since I just ran it by using mpif77 compiler. – John Taylor Jun 07 '16 at 15:24
  • 1
    It's entirely possible that `mpif77` is actually pointing at a fortran 90 (and onwards) compatible compiler however. Try running `mpif77 --version` to see what compiler it's actually using "under the hood" – d_1999 Jun 07 '16 at 15:26
  • @d_1999 : it writes: "GNU Fortran (GCC) 5.2.0". – John Taylor Jun 07 '16 at 15:28
  • 2
    This is a relatively modern version of `gfortran` which will have quite complete support for many fortran versions from fortran77 up to fortran 2008, so you should be able to use most modern language features (and avoid the old hacks). – d_1999 Jun 07 '16 at 15:30
  • @d_1999: hmm, very interesting. Thank You again! – John Taylor Jun 07 '16 at 15:32

1 Answers1

2

Your format only specifies a single float but you actually want to write N per line.

A fairly general solution for this simple case would be something like

  program temp
  implicit none
  integer, parameter :: N=3
  real, dimension(N,N) :: c
  integer :: i,j
  character(len=20) :: exFmt
  c = 1.0
  write(exFmt,'("(",I0,"(F8.3))")') N
  do i=1,N
     write(*,exFmt) (c(i,j), j=1,N)
  end do
  end program

This will make exFmt be '(3(F8.3))', which specifies printing three floats (note you probably really want '(3(F8.3," "))' to explicitly include some spacing).

Note some compilers will allow for exFmt to be just '(*(F8.3))'. This is part of the fortran 2008 specification so may not be provided by all compilers you have access to. See here for a summary of compiler support (see Unlimited format item, thanks to HighPerformanceMark for this)

Finally an easy bodge is to use a format statment like '(1000(F8.3))' where 1000 is larger than you will ever need.

d_1999
  • 854
  • 6
  • 16