replica nfl jerseysreplica nfl jerseyssoccer jerseyreplica nfl jerseys forums.silverfrost.com :: View topic - Changing the origin of an array
forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Changing the origin of an array

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
christyleomin



Joined: 08 Apr 2011
Posts: 155

PostPosted: Mon Oct 03, 2011 9:10 pm    Post subject: Changing the origin of an array Reply with quote

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
Back to top
View user's profile Send private message
mecej4



Joined: 31 Oct 2006
Posts: 1899

PostPosted: Tue Oct 04, 2011 12:53 am    Post subject: Re: Changing the origin of an array Reply with quote

christyleomin wrote:

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:

Code:
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
Back to top
View user's profile Send private message
davidb



Joined: 17 Jul 2009
Posts: 560
Location: UK

PostPosted: Tue Oct 04, 2011 6:11 pm    Post subject: Reply with quote

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 is 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).

Code:

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

_________________
Programmer in: Fortran 77/95/2003/2008, C, C++ (& OpenMP), java, Python, Perl
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group