Reputation: 17
I need to solve the inverted usual problem with the new line in fortran. Like printing on the screen a matrix for example. I'm using:
do i=1,n
do j=i,m
write(*, fmt="(f0.2, tr2)", advance="no") matrix(i,j)
end do
end do
In this way it is gonna put the whole matrix in the same line. I need to put each line in its own and I was wondering if there's a elegant way to do that. My solution is:
do i=1,n
do j=i,m
write(*, fmt="(f0.2, tr2)", advance="no") matrix(i,j)
end do
write(*, fmt="(a)") " " <---------- NOT ELEGANT
end do
Upvotes: 1
Views: 1950
Reputation: 18098
Off the top of my head, I would extract the last write statement, but that is not much more elegant than your solution:
program test
implicit none
integer,parameter :: n=3, m=3
real :: matrix(n,m)
integer :: i, j
call random_number( matrix )
do i=1,n
do j=i,m-1
write(*, fmt="(f0.2, tr2)", advance="no") matrix(i,j)
end do
write(*, fmt="(f0.2, tr2)") matrix(i,m)
end do
end program
If your compiler supports it, Fortran 2008 has a nice feature called the unlimited format item. Then, your problem can be solved with a single do
loop:
program test
implicit none
integer,parameter :: n=3, m=3
real :: matrix(n,m)
integer :: i
call random_number( matrix )
do i=1,n
write(*, fmt="(*(f0.2, tr2))") matrix(i,i:m)
enddo ! i
end program
Upvotes: 1