views:

236

answers:

5

Hello friends,

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

A: 

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 /
Carl Smotricz
A: 

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
janneb
A: 

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 /)
Ian Ross
A: 

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

is correct in fortran 2003

but if it has to be allocatable than you have to allocate(days(12))

zmi
A: 

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.

ShinTakezou