Help with declaring arrays in Fortran 90/95

In summary, the conversation is about creating a program that calculates the least squares fit line to a set of points. The person is struggling with how to read in an arbitrary number of points from the user. They initially planned to have the user enter the number of elements and then create an array with that many elements, but this approach is not allowed in Fortran. They discuss using allocatable arrays as a solution and suggest researching the syntax.
  • #1
warfreak131
188
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
  • #2
You need to learn how to use allocatable arrays. Googe it and learn the syntax. It is pretty easy.
 

What is an array in Fortran 90/95?

An array in Fortran 90/95 is a data structure that allows you to store multiple values of the same data type in a single variable. It can be thought of as a collection of elements arranged in a specific order.

How do I declare an array in Fortran 90/95?

To declare an array in Fortran 90/95, you need to specify the data type of the elements, the number of dimensions, and the size of each dimension. For example, to declare a 1-dimensional array of integers with 10 elements, you would use the syntax: integer :: my_array(10).

Can I declare an array with a variable size in Fortran 90/95?

Yes, you can declare an array with a variable size in Fortran 90/95 using the ALLOCATABLE keyword. This allows you to dynamically allocate memory for the array at runtime, based on the value of a variable.

How do I access elements of an array in Fortran 90/95?

You can access elements of an array in Fortran 90/95 by specifying the index values within parentheses after the array name. For example, to access the third element of an array called my_array, you would use my_array(3).

Can I use loops with arrays in Fortran 90/95?

Yes, you can use loops with arrays in Fortran 90/95 to perform operations on multiple elements of the array. This is a powerful feature of arrays that allows you to efficiently process large amounts of data.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
4
Views
614
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
7
Replies
235
Views
10K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
20
Views
1K
Back
Top