Reputation: 762
Can someone explain what is zero indexed array in Fortran along with example. I'm not getting any content on that on internet.
Upvotes: 4
Views: 11933
Reputation: 26471
A zero indexed array is an array who's index origin is ZERO
. This means that the first element of the array is referenced by index 0
.
Fortran arrays defaultly start with index 1 when you declare them
INTEGER, DIMENSION(3) :: v
Here, the symbol v
represents a one-dimensional array of size 3 with elements v(1)
,v(2)
and v(3)
.
However, the Fortran standard gives you the possibility to set the starting and ending index of your array. E.g.:
INTEGER, DIMENSION(0:2) :: w
In this case, the symbol w
represents again a one-dimensional array of size 3
. But now with elements w(0)
,w(1)
and w(2)
. As the starting index is 0
this is a zero indexed array.
For an explicit shape array Section 5.3.8.2 of the standard states that the DIMENSION
attribute can be declared as
DIMENSION ( [lower-bound :] upper-bound )
So anything is possible, you can start with -42
and end with +42
if you want.
The values of each
lower-bound
andupper-bound
determine the bounds of the array along a particular dimension and hence the extent of the array in that dimension. Iflower-bound
appears it specifies the lower bound; otherwise the lower bound is 1. The value of a lower bound or an upper bound may be positive, negative, or zero. The subscript range of the array in that dimension is the set of integer values between and including the lower and upper bounds, provided the upper bound is not less than the lower bound. If the upper bound is less than the lower bound, the range is empty, the extent in that dimension is zero, and the array is of zero size.
Upvotes: 15