Doubt about variables declaration

Dear friends, I have a problem declaring an integer variable. A sketch of my code goes something like this:

implicit none
integer :: a(10), i
.
.
do i = 1, 10
a(i) = a(i - 1) + 2 ! where a(0) = 0
end do

However, I don’t know how to declare a(0) = 0, because when declaring it before the loop I get this error when compiling, " Array reference at (1) is out of bounds (0 < 1) in dimension 1."

Could you please help me fix this problem? Thank you very much.

2 Likes

Others can be more rigorous, but basically in Fortran the default lower bound of arrays is 1.

If you want, you can declare non-default bounds

 integer :: a(0:10)
3 Likes

@epagone, great solution, many thanks for the help.

1 Like

You would still need an assignment “a(0)=0” before the loop.

Or the one liner “a=[ (2*i, i=0,10) ]”

4 Likes

or,

a = [ ( i, i = 0, 20, 2 ) ]

It avoids multiplication, though in this particular case, I think it is irrelevant.

2 Likes