1

I am a newbie in Fortran. Can any1 tell me how to define an integer array in prior. E.g. I want to define an array with no.of days in 12 months. like...

integer,allocatable(12,1) :: days

days=[31,28,31,30,31,30,31,31,30,31,30,31]

Is this syntax correct? If not, please let me know the correct one.

Thanks Praveen

4 Answers 4

2

If you want a dynamically allocated array, try the following:


program arraytest
  implicit none
  integer, allocatable :: a(:)

  allocate(a(12))
  a = (/31,28,31,30,31,30,31,31,30,31,30,31/)
  print *, a
end program arraytest
Sign up to request clarification or add additional context in comments.

Comments

1

integer, dimension(12) :: a = (/ 31, 28, 31, 30, ... /)

for "static" array. the [ ] instead of (/ /) is correct for Fortran 2003 and later; all the compilers I know allow that syntax even though they do not implement fully F2003. For dynamic array:

integer, dimension(:) :: a
! ...
allocate(a(12))
a = (/ .... /)
! ...
deallocate(a)

is an option too.

Comments

0

In FORTRAN 77, I'd say

  INTEGER DAYS(12) / 31,28,31,30,31,30,31,31,30,31,30,31 /

That's declaration and initialization in one.

If you want, you can also separate the two:

  INTEGER DAYS(12)
  DATA DAYS / 31,28,31,30,31,30,31,31,30,31,30,31 /

Comments

0

Probably doesn't need to be allocatable, does it, since it's just a constant array:

INTEGER :: a(12) = (/ 31,28,31,30,31,30,31,31,30,31,30,31 /)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.