LarsChung
LarsChung

Reputation: 753

2D array declaration on FORTRAN77

I'm very very new to FORTRAN, so it might be a super easy problem, but I wasn't sure after googling it on web...

Basically consider I have an input.txt file which has size of the 2D array

e.g. input.txt file contains

4 5

So after opening the file and registering the 4 and 5 on variables row and col, I do the following...

Since FORTRAN77 has a specific format where you have to declare before statements, I was thinking about using SUBROUTINE

Here is the idea of SUBROUTINE

SUBROUTINE create(row,col)
    INTEGER array(row,col)
    RETURN
END

I'm not sure if the code is able to run because it has no statements after declaration, but I already get an error saying that

Error on line x: Declaration error for array: adjustable dimension on non-argument

I'm not sure what's the problem, so I've come down to this

  1. I'm not using array declaration properly (i.e. logical error)
  2. In FORTRAN you cannot create array custom size (I don't think this will be the case...)

Sample codes and links are welcome, but summary of what I should know will be great!

In addition, FORTRAN77 will be used to compile my code, so I have to stick with FORTRAN77 style

Upvotes: 0

Views: 1865

Answers (5)

steabert
steabert

Reputation: 6878

In addition, FORTRAN77 will be used to compile my code, so I have to stick with FORTRAN77 style

FORTRAN77 is a language, it can't compile your code. Which compiler will you be using? Even though you cannot use automatic arrays or allocatable arrays with FORTRAN77, I think even compilers like g77 support them.

If you really need to stick to strict FORTRAN77, then you'll have to either:

  • allocate a huge work array and use that in subroutines
  • write your own allocator in C with a Fortran wrapper
  • use e.g. the memory allocator from the global arrays package.

However, please, listen to the other people and use allocatable arrays instead.

Upvotes: 1

Kyle Kanos
Kyle Kanos

Reputation: 3264

It appears you want an allocatable array, which is not valid in FORTRAN77. However, since it's now 2014 and there have been 4 updates since 1977 (1990, 1995, 2003, & 2008, plus a currently on-going meeting for a 2015 update), you have the freedom to avoid FORTRAN77.

What you want to do is the following:

program array_builder
   integer, allocatable :: block(:,:)
   integer :: row, col

   read(*,*) row, col
   allocate(block(row, col))

   <whatever you need to do>
   deallocate(block) 
end program array_builder

EDIT

As a bit of a hack, one option you could try is the following:

  1. Declare a 2D array at least as big as the largest value you expect & initialize it all to zero
  2. Read in row and col from the file
  3. Only use the array with the specified range: array(1:row,1:col)

This would work like so

program pre_define_array
   integer, dimension(50,50) block
   integer row, col

   read(*,*) row, col
   block = 0
   call do_stuff(block(1:row,1:col), row, col)

end program pre_define_array

subroutine do_stuff(in_array, rows, cols)
   integer rows, cols
   integer, dimension(rows, cols) in_array

   <do math here>
end subroutine do_stuff

Upvotes: 3

francescalus
francescalus

Reputation: 32366

Having the constraint "FORTRAN 77" makes this question sound like it belongs on Code Golf: see Kyle Kanos' answer and the "recompile each time" suggestion.

Under FORTRAN 77 arrays have (had) to be of size fixed at compile-time. Subprograms could take arguments of variable size but as the memory for the array would still need to be allocated somewhere you can't use this to work around the constraint (as the subroutine example in the question attempts). Under F77, then, it isn't possible to do what you want.

As a history lesson, though, I'll give a common F77-ism for this problem when recompiling isn't good. If you can convince people that you really can't use the quite valid solutions given so far there may be some possibility to expand this explanation.

Declare an array large enough for your largest problem, then do lots of bookkeeping, passing around the problem size (subset of memory actually used) etc. If you want to see an example of the pain from that time in history see the documentation of this routine.

Upvotes: 1

LarsChung
LarsChung

Reputation: 753

I found out that you can't declare an array with variables in FORTRAN77... Unless someone gives me a code that works, I believe I will have to use alternative method

Upvotes: 0

This is a problem for at least Fortran 95, not 77. It is year 2014!!!

You need to declare the array as allocatable and allocate it using the

allocate(array(rows, cols))

after you read the cols and rows from file.

That way you d not have to use the subroutine, but can, if the program logical structure supports it.

Upvotes: 3

Related Questions