Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Array Declaration

Array Declaration

Array Declaration

Hi, I am new to Fortran and using Compaq visual fortran. I tried looking up on google but couldn't resolve ths problem.

I need to declare a matrix but the dimensions of this will not be knwon till the program gets to the executable part of the code. From the executable part i get a value for say j and my matrix needs to be F(1,j). These lines in the executable part of the code
REAL, dimension(1,j):: FN
create the following error: "Error: A specification statement cannot appear in the executable section."

Is there a way to define a matrix in the beginning with the help of variables whose value is established in the executable part of the program. Please help.Thanks


RE: Array Declaration

Thanks HTH, I am going through the information. The reason i need this functionality is that I have a matrix
D=(1,1,1,0,0,0,1,1,0,0,1,0) which I have stated in my program as

D = RESHAPE((/1,1,1,0,0,0,1,1,0,0,1,0/),(/1,12/))

I want a matrix F such that it tells me the locations of the "1" in D


I tried a simple DO LOOP in my code
do i=1,12
if (D(1,i)==1) then
end if
end do

but since my matrix F is not pre-defined it gives me an error: "Error: A specification statement cannot appear in the executable section."

In order to define the matrix F in the beginning part of the code I will have to know the number of "1" in D. The DO LOOP above does it but the number od "1"s will not be known till this part of the code is executed. That is the reason I wanted to know if there is way to define a matrix in the beginning of the code but let its dimensions vary in the later part of the code. I hope this explains my madness. May be there is an easier way to do this but since I am only a week old at Fortran I don't know how to do it. Thanks


RE: Array Declaration

Ahad, the HTH is shorthand for Hope This Helps.  It has been forever since I wrote any Fortran, but do you need an array to hold the results of the location of the "1"?  

I am unfamiliar with the Function RESHAPE since I learned Fortran in 1968 and it did not exist then, but I did Google up this reference:

Maybe there is another function to do what you want.

HTH, George

I have been called "A storehouse of worthless information" many times.

RE: Array Declaration

Thanks George,
The answer to your question  "...but do you need an array to hold the results of the location of the "1"?  
is Yes. That is exactly what I need.


RE: Array Declaration

The problem is that there is no 'realloc' function in fortran.  You could try something clumsy like this


program main
   integer, dimension(1,12):: d
   integer, dimension(:), allocatable, target:: fbuf1, fbuf2
   integer, dimension(:), pointer:: f
   integer::fcount, fmax, oldmax
   D = RESHAPE((/1,1,1,0,0,0,1,1,0,0,1,0/),(/1,12/))

   fmax = 2
   fcount = 0
   print *, 'allocating'
   allocate (fbuf1(fmax))
   f => fbuf1
   do i = 1, 12, 1
      if (D(1,i) .eq. 1) then
         fcount = fcount + 1
         if (fcount .gt. fmax) then
            ! copy
            oldmax = fmax
            allocate (fbuf2(oldmax))
            do j = 1, oldmax, 1
               fbuf2(j) = fbuf1(j)
            ! reallocate
            fmax = fmax + 2
            print *, 'reallocating'
            deallocate (fbuf1)
            allocate (fbuf1(fmax))
            ! restore
            do j = 1, oldmax, 1
               fbuf1(j) = fbuf2(j)
            deallocate (fbuf2)
            f => fbuf1
         f(fcount) = i
         print '(12I3)', (f(j), j = 1, fcount, 1)
end program main
You could make the code more efficient by buffer switching so the copy is only done once.  Someone has also written a realloc function: have a look at http://www.star.le.ac.uk/~cgp/f90course/f90.html.

There is some talk about reallocate in the 2003 std but I don't have access to a 2003 compiler so I can't suggest anything about it.

RE: Array Declaration

You guys are getting way too complicated I think, for this relatively simple task.  There are 2 quick ways you can handle this situation.

1)  Declare your F() array up front to be the same size as your D() array.  F() is always big enough now.  You just need one other variable to keep track of the maximum "needed" size of F().  If D() is not a huge array,  this is probably the simplest, fastest executing, most efficient way to do things.

2) Declare F() as an allocatable array up front.  Use the COUNT function to quickly count the number of "1" entries in D().  Allocate F()  to be F(1,j) where j is the value returned by the COUNT function.   Now, use a simple loop to assign values to all the elements in F().  Once again,  unless D() is really huge,  this will be a quick, painless procedure.  This method is only marginally more complex than the first method above.

You can always use pointers as XWB did in his example ( I didn't review it closely ),  but... pointers are more error prone,  harder to understand,  and...  if F() needs to be very big at all,  pointers will slow your code down considerably.  I tend to only recommend pointer usage where there is no other viable method ( my opinion only of course ).


RE: Array Declaration

Perhaps this will help ...
You can dimension an array using integer variables.
these variables can be passed into your function or subroutine along with the value you want to assign
As a very basic example:-




To add complexity, the VALUE could alternatively be assigned by CALL functions or subroutines within the DO statements ... or maybe this is far too simple and I have not grasped your question!


RE: Array Declaration

in order to use a matrix whose dimensions are not known beforehand u can use an allocatable matrix, eg:

real, allocatable :: a(:,:)
allocate ( a(3,6) )

this creates a matrix and u get to give the dimensions u want later on. it could be like this:

integer, allocatable :: a(:,:)
write(*,*) 'input n and m matrix dimensions'
read(*,*) n, m
allocate ( a(n,m) )

this would allow you to have a matrix and input the dimensions you want every time you run the program.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close