Creating dynamically-sized arrays in Fortran 90/95

  • Context: Fortran 
  • Thread starter Thread starter warfreak131
  • Start date Start date
  • Tags Tags
    Arrays Fortran
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 4K views
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.
 
Physics news on Phys.org
You need to learn how to use allocatable arrays. Googe it and learn the syntax. It is pretty easy.