Silverfrost Forums

Welcome to our forums

Changing the origin of an array

3 Oct 2011 8:10 #9046

I'm rewriting a MathCAD to Fortran.

In the MathCAD code, the origina of the array is set as -1 (for some reason).

Is there a way in Fortran to change the origin from 1 to -1 (of an array)?

Please help!

Christy

3 Oct 2011 11:53 #9047

Quoted from christyleomin

Is there a way in Fortran to change the origin from 1 to -1 (of an array)?

I don't know what you mean by 'change the origin'. You can, however, declare an array to have any integer index range; for example:

program tst
integer x(-5:5),i
do i = 1,11
   x(i-6) = i*i
end do
write(*,'(1x,2I10)')(i,x(i),i=-5,5)
end program tst
4 Oct 2011 5:11 #9048

Each array dimension as a lower bound, an upper bound and an extent

The extent of a dimension is always equal to upper bound - lower bound + 1 (unless upper bound < lower bound, when the extent is defined as 0).

The size of an array is the product of the extents for all dimensions. For an array of rank 1, the extent of dimension 1 [u:8ee59ee42a]is[/u:8ee59ee42a] the same as the size of the array.

You can have any lower bound you like. The following code has an array a with a lower bound of -1. You need to re-assert the bound is -1 on any dumy arguments or the bound will revert back to 1 in that scope (Compare the two subroutines in the module).

module reports
contains
   subroutine report_extent1(a)
      real, intent(in) :: a(:)
      print *, 'report extent 1'
      print *, 'lower bound index = ', lbound(a,dim=1)
      print *, 'upper bound index = ', ubound(a,dim=1)
   end subroutine report_extent1

   subroutine report_extent2(a)
      real, intent(in) :: a(-1:)
      print *, 'report extent 2'
      print *, 'lower bound index = ', lbound(a,dim=1)
      print *, 'upper bound index = ', ubound(a,dim=1)
   end subroutine report_extent2
end module reports

program negative_base
   use reports
   implicit none
   
   integer i
   real a(-1:8)
      
   print *, 'lower bound index = ', lbound(a,dim=1)
   print *, 'upper bound index = ', ubound(a,dim=1)
   
   print *, 'size = ', size(a)
   
   ! Setup a with some values
   do i=lbound(a,dim=1), ubound(a,dim=1)
      a(i) = real(i)
   end do
   
   do i=lbound(a,dim=1), ubound(a,dim=1)
      print *, i, a(i)
   end do
   
   call report_extent1(a)
   
   call report_extent2(a)
   
end program negative_base
Please login to reply.