Constructing an array

Taiseer
01-13-2011, 02:32 PM
Dear all,

I have 399 number written as one columns in an ascii file.
How can I write a matrix from them.
The dimension of the matrix is 19X21.

davekw7x
01-15-2011, 09:17 AM
I have 399 number written as one columns in an ascii file...The dimension of the matrix is 19X21.
Declare the array and make nested loops to read the elements.

For example, if the array were 2x3:

IMPLICIT NONE
INTEGER, PARAMETER :: ReadUnit = 7
INTEGER, PARAMETER :: NumRows = 2
INTEGER, PARAMETER :: NumCols = 3

REAL x(NumRows, NumCols)
INTEGER i, j

open(ReadUnit, file = 'data.txt', status = 'old')
do i = 1,NumRows
do j = 1, NumCols
end do
end do

do i = 1, NumRows
do j = 1, NumCols
write(*, '("x(",I0,",",I0, ") = ", 1pe14.6)') i,j, x(i,j)
end do
end do

Here's my data.txt file:

1
2
3
42
3.1415926
-999

And here's the output (GNU gfortran version 4.1.2)

x(1,1) = 1.000000E+00
x(1,2) = 2.000000E+00
x(1,3) = 3.000000E+00
x(2,1) = 4.200000E+01
x(2,2) = 3.141593E+00
x(2,3) = -9.990000E+02

Regards,

Dave

Footnote
If you don't have a textbook, I respectfully suggest that you find an on-line tutorial that will introduce to Fortran fundamentals. I'm thinking it might be more time-effective than posting a request for help on a forum and waiting (and hoping) for a helpful response.

I mean, it's always OK to ask, but...

Just poke around the web and find a tutorial that makes sense to you. There are lots of them out there. First hit from google gave this: http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/fortran.html