fortran dynamic variables names

家住魔仙堡 提交于 2021-02-11 13:46:30

问题


I am writing a code where I need to arrays defined as u1,u2,u3. I require that number of variables defined are dictated by the user. for example if the user enters an integer value of "7". Then the variables defined are u1,u2,u3,u4,u5,u6,u7. So the variable names for the arrays are being defined by what value the user enters.


回答1:


From the description of your problem, you simply want an allocatable array.

TYPE(whatever), ALLOCATABLE :: u(:)
INTEGER :: some_number
PRINT *, 'Enter the number of things you want:'
READ *, some_number
ALLOCATE(u(some_number))
! work with u(1) through to u(some_number)

Standard Fortran does not offer dynamic variable naming "out of the box".



来源:https://stackoverflow.com/questions/28226498/fortran-dynamic-variables-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!