# Implied do loop and format specifier

**URL:** <https://fortran-lang.discourse.group/t/implied-do-loop-and-format-specifier/8780>\
**Category:** Help\
**Created:** [October 31, 2024, 2:25pm UTC](https://fortran-lang.discourse.group/t/implied-do-loop-and-format-specifier/8780 "2024-10-31T14:25:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shahid](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahid/32/3672_2.png) [@Shahid](https://fortran-lang.discourse.group/u/Shahid)\
**Post date:** [October 31, 2024, 2:25pm UTC](https://fortran-lang.discourse.group/t/implied-do-loop-and-format-specifier/8780/1 "2024-10-31T14:25:43Z")

</div>

It seems like using implied do I can not get the 2D matrix type output here

```auto
program test
    implicit none
    integer :: i,j
    real(4), dimension(4,4) :: r
    
    call random_number(r)
    
    print*, "with format specifier"
    do i=1,4
        write(*,10) (r(i,j), j=1,4)
    end do
    
    print*, "without format specifier"
    do i=1,4
        write(*,*) (r(i,j), j=1,4)
    end do
    
    10 format (f10.4)

end program test

```

The output with gfortran on windows 10 is

```auto
 with format specifier
    0.0405
    0.8949
    0.2582
    0.4270
    0.3203
    0.7786
    0.3008
    0.1856
    0.5101
    0.1429
    0.8176
    0.2664
    0.2779
    0.9987
    0.4930
    0.4464
 without format specifier
   4.05029058E-02 0.894873738 0.258236468 0.426996052
  0.320268631 0.778587878 0.300842881 0.185597003
  0.510103643 0.142944932 0.817550063 0.266373038
  0.277874827 0.998683572 0.493006110 0.446365178

```

How to get the output as **2D Matrix** using implied do construct with format specifier?

---

<div class="post-metadata">

**Author:** ![mEm](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@mEm](https://fortran-lang.discourse.group/u/mEm)\
**Post date:** [October 31, 2024, 2:50pm UTC](https://fortran-lang.discourse.group/t/implied-do-loop-and-format-specifier/8780/2 "2024-10-31T14:50:51Z")

</div>

Try changing the format specifier to `10 format (*(f10.4))`.  
`10 format (f10.4)` would always print a single value per line, regardless of whether it receives a scalar or a ND-array.

---

<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:** [October 31, 2024, 3:14pm UTC](https://fortran-lang.discourse.group/t/implied-do-loop-and-format-specifier/8780/3 "2024-10-31T15:14:04Z")

</div>

Ditto. Before non-advancing and stream I/O was used the implied do in a WRITE statement was particularly useful for printing arbitrariy numbers of values on a single line, but an asterisk repeat count is a relatively new addition, so you would see formats like “(9999(f10.4,1x))”. It might not be needed if “f10” is sufficient to always leave a space but adding an explicit “1x” makes sure values always have white-space between them.

> **[GitHub - urbanjost/M\_display: An fpm(1) package for displaying small matrices...](https://github.com/urbanjost/M_display)**
>
> An fpm(1) package for displaying small matrices based on dispmodule(3f)

if available as an fpm package if you have a lot of little arrays to print and are an fpm user(it also has a gmake file). I think the stdlib project has included procedures for printing small arrays as well.
