开发者

define integer array fortran

开发者 https://www.devze.com 2022-12-11 09:20 出处:网络
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.

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,开发者_如何学C31,30,31,30,31,31,30,31,30,31]

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

Thanks Praveen


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


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.


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 /


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 /)
0

精彩评论

暂无评论...
验证码 换一张
取 消