Print 2d array with loop

Below is my program that prints a 2d array. First, is it OK to use an empty write statement to start a new line? Second, why advance = “no” rather than advance = .false.?

program main
   real :: x(5, 5)
   integer :: i, j
   call random_number(x)
   x = x - 0.5
   do i = 1, 5
    do j = 1, 5
        write(*, "(f8.2)", advance = "no") x(i, j)
    end do
    write(*, *)
   end do
end program

You can do it that way, but it is simpler to use an unlimited format, as shown below:

program main
   real :: x(5, 5)
   integer :: i, j
   call random_number(x)
   x = x - 0.5
   do i = 1, 5
      do j = 1, 5
         write(*, "(f8.2)", advance = "no") x(i, j)
      end do
      write(*, *)
   end do
   write (*, *)
   do i=1, 5
      write (*,"(*(f8.2))") x(i,:) ! unlimited format
   end do
end program

Sample output:

    0.28   -0.01    0.09   -0.30   -0.24
    0.50   -0.20    0.28    0.19   -0.47
    0.10   -0.36    0.28   -0.20   -0.10
   -0.12   -0.45   -0.19   -0.14    0.26
   -0.29    0.34    0.33   -0.02   -0.25

    0.28   -0.01    0.09   -0.30   -0.24
    0.50   -0.20    0.28    0.19   -0.47
    0.10   -0.36    0.28   -0.20   -0.10
   -0.12   -0.45   -0.19   -0.14    0.26
   -0.29    0.34    0.33   -0.02   -0.25
3 Likes

I think the standard writers wanted to allow for such possibilities as advance='maybe'. Restricting the possibilities to only .true. or .false. might have appeared too limiting at the time it was included in the language.

1 Like

advance='maybe' ??? Sounds like this INTERCAL - Wikipedia

You can also print it like this:

write (*,"(5(f8.2))") 

This shows:

    0.47    0.49   -0.41   -0.14    0.38
    0.28   -0.03    0.07   -0.39   -0.40
   -0.22   -0.44    0.33   -0.38   -0.35
    0.38    0.03   -0.12    0.30    0.19
   -0.20   -0.02   -0.01   -0.12   -0.40

You can transpose it if you want before printing too.

For added complexity you can use a colon edit descriptor to add content between array elements, but not after the data is exhausted.

A simple example is to place a comma between array elements, but not after the last item

program main
  real :: x(5, 5)
  integer :: i
  call random_number(x)
  x = x - 0.5
  do i = 1, size(x,dim=1)
    write (*,"(*(f8.2,:,','))") x(i,:) ! unlimited format
  end do
end program

generates

   -0.16,    0.06,    0.26,   -0.07,    0.01
    0.03,   -0.01,   -0.33,   -0.18,   -0.20
   -0.30,    0.40,   -0.18,   -0.23,    0.25
    0.29,    0.27,   -0.00,    0.04,    0.44
    0.17,    0.26,    0.49,    0.32,    0.30
2 Likes

Note there are open-source modules available for printing small arrays.
For example …

M_display

If you are using fpm(1) you can just add it as a dependency

   [dependencies]
   M_display        = { git = "https://github.com/urbanjost/M_display.git" }
program main
   use M_display, only: disp
   real :: x(5, 5)
   integer :: i, j
   call random_number(x)
   x = x - 0.5

   call disp(x, fmt='f8.2')

end program
     0.16       0.08       0.50      -0.33       0.42
     0.21       0.40      -0.19       0.03       0.44
    -0.46       0.42       0.43       0.29      -0.37
    -0.34       0.23      -0.30       0.29      -0.26
    -0.23      -0.06       0.06      -0.37      -0.42
1 Like