Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting WRITE output in Fortran 1

Status
Not open for further replies.

classic35mm

Programmer
Jun 25, 2011
3
US
Hi,

I am new to Fortran. I would like to be able to construct and print a two-dimensional array. For example, I wrote the following code:

Code:
PROGRAM myarray
  IMPLICIT NONE
  INTEGER :: i, j, k
  INTEGER, DIMENSION(3,4) :: a

  k=1
  DO i=1,3
    DO j=1,4
      a(i,j)=k
      k=k+1
    END DO
  END DO

 DO i=1,3
  WRITE(*,*) (a(i,j), j=1,4)
 END DO

END PROGRAM myarray

and this seems to work correctly; I get the following output:

Code:
           1           2           3           4
           5           6           7           8
           9          10          11          12

which is correct, I think; I am basically just printing out the "actual" array. Now, what if I would like the columns to be more closely-spaced when I print the array using WRITE? Suppose that I would like each integer in the array to sit in a column of three characters in length. To try to do this, I tried the following code:

Code:
PROGRAM myarray
  IMPLICIT NONE
  INTEGER :: i, j, k
  INTEGER, DIMENSION(3,4) :: a

  k=1
  DO i=1,3
    DO j=1,4
      a(i,j)=k
      k=k+1
    END DO
  END DO

  DO i=1,3
    WRITE(*,"(I3)") (a(i,j), j=1,4)
  END DO

END PROGRAM myarray

where I have replaced the second * in WRITE with the format string "(I3)". I get the following output:
Code:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12

So, I am not using the correct format string, since my data is no longer printed in "array" form, with three rows and four columns. Also, oddly, I am having some trouble finding a good, detailed description of Fortran's format strings and how they can be used. Do you know of any good resources for this online?

Thank you very much.
 
Try something like :

Code:
  DO i=1,3
    WRITE(*,"(100(1X,I3))") (a(i,j), j=1,4)
  END DO

You may replace 100 by any value greater than or equal to the number of columns. 1X is there to imposed at least a space between two values. Instead of I3, I0 may be a better solution (I3 = 3 digits whereas I0 means as many digits as necessary)


François Jacq
 
Hi all, I am new to Fortran too.

the proposed solution works, but there is any method to set the number of columns from the input?

what I am looking for is a way to let work

Code:
integer ::a, i ,j
read (*,*) a

do i = 1,1092

write(12,'a(fw.d)') (1.0, j =1,a)

...
 
blasco - it is better to start a new thread for your own question instead of tagging on to someone elses otherwise the advisers and the ops get confused by the cross talk.
 
@blasco :

It is possible to build up a format dynamically :

Code:
program test
character(50) :: form
integer :: n, j
read (*,*) n
write(form,"(A,I0,A)") "(",n,"(1X,F7.2))"
write(12,form) (1.0, j =1,n)
end


François Jacq
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top