5 Fortran Arrays

5.1 array types, 5.1.1 explicit shape arrays, 5.1.2 assumed shape arrays, 5.1.3 deferred shape arrays, 5.1.4 assumed size arrays, 5.2 array specification, explicit shape arrays, assumed shape arrays, deferred shape arrays, assumed size arrays, 5.3 array subscripts and access, 5.3.1 array sections and subscript triplets, 5.3.2 array sections and vector subscripts, 5.4 array constructors, 5.5 pgi array extensions (cm fortran) @, 5.5.1 the array attribute (cm fortran) @, 5.5.2 array constructors extensions (cm fortran) @.

Fortran Tutorial

  • Fortran Tutorial
  • Fortran - Home
  • Fortran - Overview
  • Fortran - Environment Setup
  • Fortran - Basic Syntax
  • Fortran - Data Types
  • Fortran - Variables
  • Fortran - Constants
  • Fortran - Operators
  • Fortran - Decisions
  • Fortran - Loops
  • Fortran - Numbers
  • Fortran - Characters
  • Fortran - Strings

Fortran - Arrays

  • Fortran - Dynamic Arrays
  • Fortran - Derived Data Types
  • Fortran - Pointers
  • Fortran - Basic Input Output
  • Fortran - File Input Output
  • Fortran - Procedures
  • Fortran - Modules
  • Fortran - Intrinsic Functions
  • Fortran - Numeric Precision
  • Fortran - Program Libraries
  • Fortran - Programming Style
  • Fortran - Debugging Program
  • Fortran Resources
  • Fortran - Quick Guide
  • Fortran - Useful Resources
  • Fortran - Discussion
  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary

Arrays can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

Arrays can be one- dimensional (like vectors), two-dimensional (like matrices) and Fortran allows you to create up to 7-dimensional arrays.

Declaring Arrays

Arrays are declared with the dimension attribute.

For example, to declare a one-dimensional array named number, of real numbers containing 5 elements, you write,

The individual elements of arrays are referenced by specifying their subscripts. The first element of an array has a subscript of one. The array numbers contains five real variables –numbers(1), numbers(2), numbers(3), numbers(4), and numbers(5).

To create a 5 x 5 two-dimensional array of integers named matrix, you write −

You can also declare an array with some explicit lower bound, for example −

Assigning Values

You can either assign values to individual members, like,

or, you can use a loop,

One-dimensional array elements can be directly assigned values using a short hand symbol, called array constructor, like,

please note that there are no spaces allowed between the brackets ‘( ‘and the back slash ‘/’

The following example demonstrates the concepts discussed above.

When the above code is compiled and executed, it produces the following result −

Some Array Related Terms

The following table gives some array related terms −

Passing Arrays to Procedures

You can pass an array to a procedure as an argument. The following example demonstrates the concept −

In the above example, the subroutine fillArray and printArray can only be called with arrays with dimension 5. However, to write subroutines that can be used for arrays of any size, you can rewrite it using the following technique −

Please note that the program is using the size function to get the size of the array.

Array Sections

So far we have referred to the whole array, Fortran provides an easy way to refer several elements, or a section of an array, using a single statement.

To access an array section, you need to provide the lower and the upper bound of the section, as well as a stride (increment), for all the dimensions. This notation is called a subscript triplet:

When no lower and upper bounds are mentioned, it defaults to the extents you declared, and stride value defaults to 1.

The following example demonstrates the concept −

Array Intrinsic Functions

Fortran 90/95 provides several intrinsic procedures. They can be divided into 7 categories.

Vector and matrix multiplication

Construction

Manipulation

Fortran 90: Array Operations

Array sections.

One-dimensional Arrays

Here, the three arrays A, B, and C have each been dimensioned with 10 storage slots. The index of the real arrays A and B start at 1 while the index for the integer array C starts at 0.

assigns the previously determined values of the elements of the A array to the array B.

assigns the ith element of A the value of the sum of the ith elements of arrays A and B. Similarly, the ith element of C is assigned the value equal to the ith element of itself multiplied by 2.

For example, if A is assigned the values

then, we may consider assigning the elements of the array B as

This assigns to B the values 0, 0, 0, 0, 0, 1, 1, 1, 1, 1.

  • Several intrinsic array-type functions are available for processing arrays. Some of these include DOT_PRODUCT(A, B): returns the dot product of A and B MAXVAL(A): returns the maximum value in array A MAXLOC(A): returns a one-element 1D array whose value is the location of the first occurrence of the maximum value in A PRODUCT(A): returns the product of the elements of A SUM(A): returns the sum of the elements of A

At run time, the actual bounds for the array A may be determined by the statement

where N is an integer variable that has been previously assigned. To ensure that enough memory is available to allocate space for your array, make use of the STAT option of the ALLOCATE command:

Here, AllocateStatus is an integer variable. AllocateStatus takes the value 0 if allocation is successful or some other machine dependent value of there is insufficient memory.

An array can be released from memory by using the DEALLOCATE command

Again, DeAllocateStatus is an integer variable whose value is 0 if the deallocation was successful.

Multi-dimensional arrays

The maximum limit on the rank (the number of dimensions) of an array is 7.

This assigns the values of the A array in column order similar to the rules of Fortran 77.

  • Just like with one-dimensional arrays, operators and functions normally applied to simple expressions may also be applied to multi-dimensional arrays having the same number of elements. Such operations are carried out on an element by element basis.
  • A WHERE construct may be used to assign values to the individual elements of an array with WHERE ( logical argument ) sequence of array assignments ELSEWHERE sequence of array assignments END WHERE
  • Several intrinsic array-type functions are available for processing multi-dimensional arrays. Some of the more useful ones are MAXVAL(A, D): returns an array of one less dimension than A containing the maximum values of A along dimension D (if D is omitted, returns the maximum value in the entire array) MAXLOC(A): returns a one-element 1D array whose value is the location of the first occurrence of the maximum value in A SUM(A, D): returns an array of one less dimension than A containing the sums of the elements of A along dimension D (if D is omitted, returns sum of the elements in the entire array) MATMUL(A, B): returns the matrix product of A and B TRANSPOSE(A): returns the transpose of the 2D array A

At run time, the actual bounds for the array A may be determined by the statements

Here, N and AllocateStatus are integer variables. AllocateStatus takes the value 0 if allocation is successful or some other machine dependent value of there is insufficient memory.

Copyright © 1996-7 by Stanford University. All rights reserved.

IMAGES

  1. Fortran 90 Arrays

    fortran 90 array assignment

  2. PPT

    fortran 90 array assignment

  3. Fortran 90

    fortran 90 array assignment

  4. Fortran 90

    fortran 90 array assignment

  5. An example of Fortran 90 code.

    fortran 90 array assignment

  6. Figure A1. Fortran 90 code defining the sinusoid analytical function

    fortran 90 array assignment

VIDEO

  1. Fortran Catapult

  2. HOW TO ALLOCATE AN ARRAY IN FORTRAN ?

  3. Lecture 7

  4. Fortran 90

  5. Advanced Fortran Programming : 028 : Processing Files

  6. Algorithm: Write a Fortran program to find the sum of the following series

COMMENTS

  1. Fortran 90 Arrays - Michigan Technological University

    Fall 2009. zA Fortran 90 program uses the DIMENSION attribute to declare arrays. zThe DIMENSION attribute requires three components in order to complete an array specification, rank, shape, and extent. zThe rank of an array is the number of “indices” or “subscripts.”. The maximum rank is 7 (i.e., seven-dimensional).

  2. Fortran: initializing of and assigning value to arrays

    This defines the entire array. There is no need to designate array sections in this case, since you are calling out the entire array: r (:, :). Steve Lionel has a discussion of why trying to make arrays obvious with ":" isn't a good idea -- there are differences between array and array (:).

  3. Arrays - Colorado State University

    This indicates that the shape of the dummy array is to be taken from the actual argument used when the procedure is called. This is known as an assumed-shape array. Example: rhvals2.f90. The declaration of arrays may also use values of other dummy arguments to establish extents. These are called automatic arrays.

  4. Array Concepts - Colorado State University

    Array Constructors (1) Commonly used for assigning array values An array constructor will create a temporary array INTEGER, DIMENSION(6) :: marks marks = (/ 10, 25, 32, 54, 56, 60 /) Constructs an array with the elements 10, 25, 32, 54, 56, 60 And then copies that array into marks Fortran 2003 addition: Also can use square brackets

  5. 5 Fortran Arrays - physics.bgsu.edu

    Fortran arrays are any object with the dimension attribute. In Fortran 90, and in HPF, arrays may be very different from arrays in older versions of Fortran. Arrays can have values assigned as a whole without specifying operations on individual array elements, and array sections can be accessed.

  6. Fortran - Arrays - Online Tutorials Library

    Fortran - Arrays. Arrays can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. All arrays consist of contiguous memory locations.

  7. Fortran 90: Array Operations - University of Illinois Urbana ...

    Fortran 90: Array Operations. Let's start with a simple example. Suppose we wish to add two arrays A and B and put the result in C. In Fortran 77 , we would have something like. do i=1,n do j=1,n C (j,i) = A (j,i) + B (j,i) enddo enddo. In Fortran 90, it is as simple as C = A + B . Most of the intrinsic functions operate component-wise on arrays.

  8. 7.2 Fortran 90 - Argonne National Laboratory

    7.2.1 Array Assignment Statement. F90 allows a variety of scalar operations---operations defined on single values---to be applied also to entire arrays. This feature causes the scalar operation to be applied to each element of the array. If an operation involves several values, all must be represented by conformable arrays, that is, scalar ...

  9. Fortran 90 Tutorial - Department of Atmospheric and Oceanic ...

    A = A + B C = 2*C. assigns the ith element of A the value of the sum of the ith elements of arrays A and B. Similarly, the ith element of C is assigned the value equal to the ith element of itself multiplied by 2. A WHERE construct may be used to assign values to the individual elements of an array with.

  10. Fortran 90, Arrays, QUB">Introduction to Fortran 90, Arrays, QUB

    The fourth assignment is illegal because the two arrays are not conformable. 4.6.2 Array section assignment In case that sections of an array have to be assigned certain values conforming array sections may appear in the expressions. Consider the following example: REAL, DIMENSION(10) :: alpha, beta. REAL :: gamma(20) alpha(1:5)=2.0