classic35mm
Programmer
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:
and this seems to work correctly; I get the following output:
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:
where I have replaced the second * in WRITE with the format string "(I3)". I get the following output:
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.
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.