0

I'd like to know if a loop can be created, inside which I can call a subroutine in which there are arrays to be defined whose size varies as a function of loop variable. I tried as following, but got error "array bound is not scalar integer". How to solve this issue?

.
.
.
iloop: do i=5,24,0.5
jloop: do j=5,20 
call check(i,j,divlen,Machexit,final)
if (final.eq.1) exit iloop
enddo jloop
enddo iloop
.
.
end program

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Subroutine check(i,j,divlen,Machexit,final)
INTEGER, PARAMETER :: IVLpts=10 
real :: i,divlen,Machexit
integer :: final,j
integer :: exppts,intstrtpts,contourstrtpts,totalpts,P1,P2,P3,P4,P5,P6,P7
exppts=((j*IVLpts)+((j-1)*(IVLpts-1)))
P2=(exppts-IVLpts)
P3=(IVLpts-1)
P6=(exppts-P2)

call check2(IVLpts,i,j,divlen,Machexit,final,exppts,P2,P3,P6)

End subroutine check

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Subroutine check2(IVLpts,i,j,divlen,Machexit,final,exppts,P2,P3,P6)
Real, PARAMETER :: gamma=1.4d0,Mini=1.02d0
integer,allocatable :: expcontourpts(:),M(:),x(:),y(:)
real,allocatable :: AoverAstar(:),Mvariance(:)
allocate(expcontourpts(exppts))
allocate(M(exppts))
allocate(x(exppts))
allocate(y(exppts))
allocate(AoverAstar(P6))
allocate(Mvariance(P6))
.
.
.
End subroutine check2

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

6
  • 1
    Are these procedures in a host scope that has implicit none? If not there are a whole heap of things that will default to being REAL, some of which you are using as array indices... Commented Feb 6, 2014 at 8:59
  • thanks for the help...but, the host scope has an implicit none prescribed in it....dint include it in the snippet given here.... Commented Feb 6, 2014 at 9:44
  • Sorry but, like IanH, I don't see the declaration of arguments of check2, in particular exppts and p6 : they are real values by default and cannot be used as array dimension. I also suspect that you did not put your subroutines within a module starting by IMPLICIT NONE. If they are outside any host, then they are independent and should have IMPLICIT NONE inside. Commented Feb 6, 2014 at 10:45
  • Which line generates the error message? Commented Feb 6, 2014 at 14:22
  • it was basically the array definitions....since i missed to deallocate the allocated arrays...!!! so after adding the 'deallocation' part and some minor changes, the code is now running, though there are some glitches which it shows.... Thankyou all for all the discussions and help.... Commented Feb 7, 2014 at 10:20

1 Answer 1

1

Here is what I'm answering:

I'd like to know if a loop can be created, inside which I can call a subroutine in which there are arrays to be defined whose size varies as a function of loop variable.

The answer to this is "yes", but allocate() isn't the way I would do it. Just use one of the arguments as the array dimension. Here is an example that does a summation in a very inefficient way, but uses the method I'm describing:

program main
  implicit none
  integer :: i, j

  do i = 1,5
    call sum_values(i,j)
    write(*,*) j
  end do

end program main

subroutine sum_values(i,j)
  implicit none
  integer, intent(in) :: i
  integer, intent(out) :: j
  integer, dimension(i) :: an_array

  an_array(1:i) = 1
  j = sum(an_array(1:i))

end subroutine sum_values

Critical line is:

  integer, dimension(i) :: an_array

The dimension of the array was given in the argument list when it was called.

I see that you're using a different approach. That approach should still work, but I think the way I'm describing is a lot less error-prone and appropriate for the complexity of what you've asked for. Make sure to use "implicit none" statements, and declare in/out for all the arguments.

Sign up to request clarification or add additional context in comments.

9 Comments

giving as dimension(i) gives error since i changes it's values everytime....thats why i had to go for allocation-deallocation of the arrays....which in now working, though there are some other minor glitches associated....
@Sathish The code I posted compiles and runs, so why would it not work in yours? The variable i should be declared before you use it in the array dimension, and it should have the intent(in) parameter. Perhaps your code lacked that, and that's why it didn't work. Being mixed in with other errors could make it more difficult, but both methods do work and are commonly used by other people.
@AlanSE I'm quite surprised this works. What compiler ??
@george gfortran by default. I also test things on ifort and g95, but I haven't checked this. I know it will work on those compilers. This approach is very standard fortran. Let me know if the code I posted doesn't work on yours, but I'm quite confident it will.
ok, see here stackoverflow.com/questions/6617015/…, and +1... Anybody know which fortran version introduced this sort of implicit allocation?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.