New to FORTRAN - Reformatting One Column to Multiple Columns
Author Message
New to FORTRAN - Reformatting One Column to Multiple Columns

I am fairly new to fortran and have a question.  I am trying to take
one column of data and make multiple columns from it.  For instance,
if I had the numbers 1 to 10 in a column I am trying to read those
numbers in and write out 2 columns, the first with values 1-5 and the
second with 6-10.  Is there any easy way to do this with a column that
has roughly 5000 values in it?  Thanks!

Drew

Wed, 01 Sep 2010 09:27:36 GMT
New to FORTRAN - Reformatting One Column to Multiple Columns

Quote:
> I am fairly new to FORTRAN and have a question. ?I am trying to take
> one column of data and make multiple columns from it. ?For instance,
> if I had the numbers 1 to 10 in a column I am trying to read those
> numbers in and write out 2 columns, the first with values 1-5 and the
> second with 6-10. ?Is there any easy way to do this with a column that
> has roughly 5000 values in it? ?Thanks!

> Drew

1. Suppose you were insructed to use a one dimensional array. How
might you solve this problem?
2. How about with a two dimensional array?
3. Remember that Fortran arrays are column-major.

- e

Wed, 01 Sep 2010 11:38:13 GMT
New to FORTRAN - Reformatting One Column to Multiple Columns

Quote:

> I am fairly new to FORTRAN and have a question.  I am trying to take
> one column of data and make multiple columns from it.  For instance,
> if I had the numbers 1 to 10 in a column I am trying to read those
> numbers in and write out 2 columns, the first with values 1-5 and the
> second with 6-10.  Is there any easy way to do this with a column that
> has roughly 5000 values in it?  Thanks!

assuming the size of the array is an even number N, can you do something
like this for printing the array?

offset = N/2;
do i=1,N/2
write(*,*) array(i), array(i+offset)
end do

hth
raju
--
Kamaraju S Kusumanchi
http://www.people.cornell.edu/pages/kk288/
http://malayamaarutham.blogspot.com/

Thu, 02 Sep 2010 00:31:24 GMT
New to FORTRAN - Reformatting One Column to Multiple Columns

Quote:
>I am fairly new to FORTRAN and have a question.  I am trying to take
> one column of data and make multiple columns from it.  For instance,
> if I had the numbers 1 to 10 in a column I am trying to read those
> numbers in and write out 2 columns, the first with values 1-5 and the
> second with 6-10.  Is there any easy way to do this with a column that
> has roughly 5000 values in it?  Thanks!

> Drew

I interprete your homework problem as a file reorganization,
whose solution is likely of interest to others here.
Fortran provides faster ways to process a input TEXT file,
but posting a stream input solution would draw criticism.
! -----------------------
program reorganize_file
implicit none
integer :: i, n
integer,allocatable :: a(:)

open (11,file='col_1.txt')
n = 0
do
read (11,*,end=101) ; n = n+1   ! count records
end do
101 rewind (11)
n = n/2*2              ! only process even # of records
allocate ( a(n) )
close (11)
open (11,file='col_2.txt')
do i = 1,n/2           ! reorganize output into 2 columns
write (11,91) a(i), a(i+n/2)
write (*,91)  a(i), a(i+n/2)  ! optional to console
end do
91 format (2(i0,1x))
end program

Wed, 01 Sep 2010 19:39:07 GMT
New to FORTRAN - Reformatting One Column to Multiple Columns
(snip)

Quote:
> open (11,file='col_1.txt')
> n = 0
> do
>    read (11,*,end=101) ; n = n+1   ! count records
> end do
> 101 rewind (11)

This makes the program unusable on non-seekable devices,
such as pipes.   Also, the OP requested about 5000 lines
which isn't all that much for even the smallest computer
today.  (Maybe people still running CP/M won't agree.)

too big, buffer.  If the data fits, all is fine.  If not,
continue counting, rewind, and allocate the appropriate array.

The unix tail program (print the last n lines of a file) has
a similar problem, and is often used with pipes.  With
seekable input it can use tricks with file positioning to
find the right spot in the file.  With non-seekable input
the only possibility is a buffer, and there is no way to know
what size it should be.  Most versions give you only the
buffer full of data if it doesn't all fit.  (There is no
line length limit to unix text files.)

-- glen

Fri, 03 Sep 2010 06:09:26 GMT

 Page 1 of 1 [ 5 post ]

Relevant Pages