定义 fortran 整数数组

发布于 2024-08-10 00:04:34 字数 224 浏览 7 评论 0原文

我是 Fortran 新手。 any1 能告诉我如何定义一个整数数组吗? 例如 我想定义一个包含 12 个月内的天数的数组。 就像...

integer,allocatable(12,1) :: days

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

这个语法正确吗?如果不是,请告诉我正确的。

谢谢 普拉文

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

假情假意假温柔 2024-08-17 00:04:34

如果您想要动态分配的数组,请尝试以下操作:


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

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
七月上 2024-08-17 00:04:34

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

对于“静态”数组。对于 Fortran 2003 及更高版本,[ ] 而不是 (/ /) 是正确的;我知道的所有编译器都允许该语法,即使它们没有完全实现 F2003。对于动态数组:

integer, dimension(:) :: a
! ...
allocate(a(12))
a = (/ .... /)
! ...
deallocate(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.

平安喜乐 2024-08-17 00:04:34

在 FORTRAN 77 中,我想说

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

这是声明和初始化合二为一。

如果需要,您也可以将两者分开:

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

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 /
喵星人汪星人 2024-08-17 00:04:34

可能不需要可分配,是吗,因为它只是一个常量数组:

INTEGER :: a(12) = (/ 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 /)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文