Fortran Can Fortran 90 Arrays Be Dynamically Allocated Based on a Variable?

  • Thread starter Thread starter truparel
  • Start date Start date
  • Tags Tags
    Array Fortran
Click For Summary
The discussion revolves around dynamically defining arrays in Fortran based on a previously defined variable. The key point is the use of "allocatable" arrays, which allows for the creation of a variable number of arrays depending on the value of a variable, such as 'a'. For instance, if 'a' equals 2, two arrays can be allocated; if 'a' equals 3, three arrays can be allocated. The conversation encourages readers to explore the concept of allocatable arrays and provides a hint that implementing this in Fortran is straightforward. The focus is on understanding how to effectively manage memory allocation for arrays based on conditional logic.
truparel
Messages
1
Reaction score
0
Hello forum,

I was wondering if there is a way to declare/define arrays depending on some previously defined variable.

example:

if a=2, then declare/define 2 arrays
if a=3, then declare/define 3 arrays

If doable, how does one assign names to these arrays?
 
Technology news on Phys.org
what you can do is declared all 3 arrays as "allocatable", then, depending on a=2 or a=3, you go ahead and allocate as necessary.

please read up on "allocatable" and take a shot at some Fortran code. Hint: it's very, very simple.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
8K