# Print 2d array with loop

**URL:** <https://fortran-lang.discourse.group/t/print-2d-array-with-loop/6115>\
**Category:** Help\
**Created:** [July 8, 2023, 9:53pm UTC](https://fortran-lang.discourse.group/t/print-2d-array-with-loop/6115 "2023-07-08T21:53:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fortran4r](https://avatars.discourse-cdn.com/v4/letter/f/9de0a6/32.png) [@fortran4r](https://fortran-lang.discourse.group/u/fortran4r)\
**Post date:** [July 8, 2023, 9:53pm UTC](https://fortran-lang.discourse.group/t/print-2d-array-with-loop/6115/1 "2023-07-08T21:53:36Z")

</div>

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.?

```auto
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

```

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [July 8, 2023, 10:11pm UTC](https://fortran-lang.discourse.group/t/print-2d-array-with-loop/6115/2 "2023-07-08T22:11:35Z")

</div>

You can do it that way, but it is simpler to use an [unlimited format](https://stevelionel.com/drfortran/2009/07/01/doctor-fortran-in-revert-revert-the-end-of-the-format-is-nigh/), as shown below:

```auto
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:

```auto
    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

```

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [July 8, 2023, 10:48pm UTC](https://fortran-lang.discourse.group/t/print-2d-array-with-loop/6115/3 "2023-07-08T22:48:09Z")

</div>

> [@fortran4r](#):
>
> Second, why advance = “no” rather than advance = .false.?

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.

---

<div class="post-metadata">

**Author:** ![tyranids](https://avatars.discourse-cdn.com/v4/letter/t/3e96dc/32.png) [@tyranids](https://fortran-lang.discourse.group/u/tyranids)\
**Post date:** [July 8, 2023, 11:28pm UTC](https://fortran-lang.discourse.group/t/print-2d-array-with-loop/6115/4 "2023-07-08T23:28:14Z")

</div>

> [@RonShepard](#):
>
> 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.

`advance='maybe'` ??? Sounds like this [INTERCAL - Wikipedia](https://en.wikipedia.org/wiki/INTERCAL)

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [July 9, 2023, 4:31am UTC](https://fortran-lang.discourse.group/t/print-2d-array-with-loop/6115/5 "2023-07-09T04:31:17Z")

</div>

> [@Beliavsky](#):
>
> ```auto
> 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
> 
> ```

You can also print it like this:

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

```

This shows:

```console
    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.

---

<div class="post-metadata">

**Author:** ![DavidB](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@DavidB](https://fortran-lang.discourse.group/u/DavidB)\
**Post date:** [July 9, 2023, 9:07am UTC](https://fortran-lang.discourse.group/t/print-2d-array-with-loop/6115/6 "2023-07-09T09:07:53Z")

</div>

For added complexity you can use a [colon edit descriptor](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2023-0/colon-editing.html) 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

```auto
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

```auto
   -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

```

---

<div class="post-metadata">

**Author:** ![urbanjost](https://avatars.discourse-cdn.com/v4/letter/u/0ea827/32.png) [@urbanjost](https://fortran-lang.discourse.group/u/urbanjost)\
**Post date:** [July 9, 2023, 11:01am UTC](https://fortran-lang.discourse.group/t/print-2d-array-with-loop/6115/7 "2023-07-09T11:01:55Z")

</div>

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

[M\_display](https://github.com/urbanjost/M_display.git)

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

```toml
   [dependencies]
   M_display = { git = "https://github.com/urbanjost/M_display.git" }

```

```fortran
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

```

```plaintext
     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

```
