Help with declaring arrays in Fortran 90/95

  • Context: Fortran 
  • Thread starter Thread starter warfreak131
  • Start date Start date
  • Tags Tags
    Arrays Fortran
Click For Summary
SUMMARY

The discussion focuses on declaring arrays in Fortran 90/95, specifically the challenges of creating arrays with a user-defined size. The user initially attempts to declare arrays with a dynamic size using the syntax real, dimension(N) :: xvals, yvals, which leads to errors due to Fortran's requirement for variable declarations before any read/write operations. The solution is to utilize allocatable arrays, allowing for dynamic memory allocation based on user input. The user is advised to research the syntax for allocatable arrays to resolve their issue effectively.

PREREQUISITES
  • Understanding of Fortran 90/95 syntax
  • Familiarity with array declarations in Fortran
  • Knowledge of dynamic memory allocation concepts
  • Basic programming skills for handling user input
NEXT STEPS
  • Learn about Fortran 90/95 allocatable arrays syntax
  • Research the use of the allocate statement in Fortran
  • Explore examples of dynamic array handling in Fortran
  • Study error handling techniques in Fortran for read/write operations
USEFUL FOR

This discussion is beneficial for Fortran developers, students learning numerical methods, and anyone needing to handle dynamic arrays in Fortran 90/95 for data processing tasks.

warfreak131
Messages
186
Reaction score
0
I have an assignment to calculate the least squares fit line to a set of points, and I have to read in an arbitrary number of points from the user

So my plan was to have the user enter how many elements they have, and then create an array with that many elements. So let's say I read the value N from the user, and then create an array like:

Code:
real, dimension(N) :: xvals, yvals

But that's a no-no according to fortran. If any read/write statement goes before declaring the variables, I get a whole host of errors. However, if I remove those read/write statements and create a set number of array elements like:

Code:
real, dimension(10) :: xvals, yvals

then all the error go away. Unfortunately, I have to find out how many variables the user wants before creating the array.
 
Technology news on Phys.org
You need to learn how to use allocatable arrays. Googe it and learn the syntax. It is pretty easy.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 5 ·
Replies
5
Views
9K
Replies
7
Views
3K
  • · Replies 19 ·
Replies
19
Views
7K