Sathish
Sathish

Reputation: 1

Over-riding FORTRAN error "array bound is not scalar integer"

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

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

Upvotes: 0

Views: 373

Answers (1)

AlanSE
AlanSE

Reputation: 2785

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.

Upvotes: 1

Related Questions