Arrays are a central object in Fortran. The creation of dynamic sized arrays is discussed in the allocatable arrays section .

To pass arrays to procedures four ways are available

assumed-shape arrays

assumed-rank arrays

explicit-shape arrays

assumed-size arrays

The preferred way to pass arrays to procedures is as assumed-shape arrays

Higher-dimensional arrays can be passed in a similar way.

The array is simply passed by

In this case no array copy is done, which has the advantage that the shape and size information is automatically passed along and checked at compile and optionally at runtime. Similarly, array strides can be passed without requiring a copy of the array but as assumed-shape descriptor:

This should always be your default way of passing arrays in and out of subroutines. Avoid passing arrays as whole slices, as it obfuscates the actual intent of the code:

In case more general arrays should be passed to a procedure the assumed-rank functionality introduced in the Fortran 2018 standard can be used

The actual rank can be queried at runtime using the select rank construct. This easily allows to create more generic functions that have to deal with different array ranks.

Explicit-shape arrays can be useful for returning data from functions. Most of their functionality can be provided by assumed-shape and assumed-rank arrays but they find frequent use for interfacing with C or in legacy Fortran procedures, therefore they will be discussed briefly here.

To use explicit-shape arrays, the dimension has to be passed explicitly as dummy argument like in the example below

For high-dimensional arrays additional indices have to be passed.

The routines can be invoked by

Note that the shape is not checked, so the following would be legal code that will potentially yield incorrect results:

In this case the memory layout is preserved but the shape is changed. Also, explicit-shape arrays require contiguous memory and will create temporary arrays in case non-contiguous array strides are passed.

To return an array from a function with explicit-shape use

Finally, there are assumed-size arrays, which provide the least compile-time and run-time checking and can be found frequently in legacy code. They should be avoided in favour of assumed-shape or assumed-rank arrays. An assumed-size array dummy argument is identified by an asterisk as the last dimension, this disables the usage of this array with many intrinsic functions, like size or shape .

To check for the correct size and shape of an assumed-shape array the size and shape intrinsic functions can be used to query for those properties

Note that size returns the total size of all dimensions. To obtain the shape of a specific dimension add it as second argument to the function.

Arrays can be initialized by using an array constructor

The array constructor can be annotated with the type of the constructed array

Implicit do loops can be used inside an array constructor as well

In order for the array to start with different index than 1, do:

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

To Continue Learning Please Login

  • Arrays Syntax & sections

Dynamic memory allocation

Array intrinsic functions, arrays in fortran.

  • other arrays with up to 7 dimensions.

Arrays Syntax & arrays sections

Arrays syntax, array sections.

  • memory allocation is static
  • memory allocation becomes dynamic
  • Array variable has an ALLOCATABLE (or POINTER) attribute, and memory is allocated through the ALLOCATE statement, and freed through DEALLOCATE
  • a variable, which is declared in the procedure with size information coming from the argument list or form a module, is an automatic array - no ALLOCATE is needed, neither DEALLOCATE
  • Masking and use of array (intrinsic) functions is often accompanied with the use of FORALL and WHERE array statements.

Many scientific computations use vectors and matrices. The data type Fortran uses for representing such objects is the array . A one-dimensional array corresponds to a vector, while a two-dimensional array corresponds to a matrix. To fully understand how this works in Fortran 77, you will have to know not only the syntax for usage, but also how these objects are stored in memory in Fortran 77.

One-dimensional arrays

The simplest array is the one-dimensional array, which is just a sequence of elements stored consecutively in memory. For example, the declaration

declares a as a real array of length 20. That is, a consists of 20 real numbers stored contiguously in memory. By convention, Fortran arrays are indexed from 1 and up. Thus the first number in the array is denoted by a(1) and the last by a(20) . However, you may define an arbitrary index range for your arrays using the following syntax:

Here, b is exactly similar to a from the previous example, except the index runs from 0 through 19. weird is an array of length 237-(-162)+1 = 400.

The type of an array element can be any of the basic data types. Examples:

Each element of an array can be thought of as a separate variable. You reference the i'th element of array a by a(i) . Here is a code segment that stores the 10 first square numbers in the array sq :

A common bug in Fortran is that the program tries to access array elements that are out of bounds or undefined. This is the responsibility of the programmer, and the Fortran compiler will not detect any such bugs!

Two-dimensional arrays

Matrices are very important in linear algebra. Matrices are usually represented by two-dimensional arrays. For example, the declaration

defines a two-dimensional array of 3*5=15 real numbers. It is useful to think of the first index as the row index, and the second as the column index. Hence we get the graphical picture:

Two-dimensional arrays may also have indices in an arbitrary defined range. The general syntax for declarations is:

The total size of the array is then

It is quite common in Fortran to declare arrays that are larger than the matrix we want to store. (This is because Fortran does not have dynamic storage allocation.) This is perfectly legal. Example:

The elements in the submatrix A(1:3,4:5) are undefined. Do not assume these elements are initialized to zero by the compiler (some compilers will do this, but not all).

Storage format for 2-dimensional arrays

Fortran stores higher dimensional arrays as a contiguous sequence of elements. It is important to know that 2-dimensional arrays are stored by column . So in the above example, array element (1,2) will follow element (3,1). Then follows the rest of the second column, thereafter the third column, and so on.

Consider again the example where we only use the upper 3 by 3 submatrix of the 3 by 5 array A(3,5) . The 9 interesting elements will then be stored in the first nine memory locations, while the last six are not used. This works out neatly because the leading dimension is the same for both the array and the matrix we store in the array. However, frequently the leading dimension of the array will be larger than the first dimension of the matrix. Then the matrix will not be stored contiguously in memory, even if the array is contiguous. For example, suppose the declaration was A(5,3) instead. Then there would be two "unused" memory cells between the end of one column and the beginning of the next column (again we are assuming the matrix is 3 by 3).

This may seem complicated, but actually it is quite simple when you get used to it. If you are in doubt, it can be useful to look at how the address of an array element is computed. Each array will have some memory address assigned to the beginning of the array, that is element (1,1). The address of element (i,j) is then given by

where lda is the leading (i.e. row) dimension of A . Note that lda is in general different from the actual matrix dimension. Many Fortran errors are caused by this, so it is very important you understand the distinction!

Multi-dimensional arrays

Fortran 77 allows arrays of up to seven dimensions. The syntax and storage format are analogous to the two-dimensional case, so we will not spend time on this.

The dimension statement

There is an alternate way to declare arrays in Fortran 77. The statements

are equivalent to

This dimension statement is considered old-fashioned style today.

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

Array and Matrix

There is intrinsic SIZE function to determine the length of a vector, SIZE(vec). Another example where we use implied DO loop to assign an array:

Dynamic array allocation with ALLOCATABLE:

See dynamic array allocation in this example .

Assign high dimensional array with RESHAPE:

Matrix multiplication:

You can assign all the array element same value by assigning it a scaler value:

Yutaka Masuda

February 2020

Back to index.html .

Use of 1-dimensional array

Array basics, small examples.

Fortran can manage a set of variables by a single name. In other words, a single object can have multiple variables with the same type. This object is called array , and each variable in the array is element . The array is often explained as a 1-dimensional structure with multiple variables.

This structure is useful to define a vector that has multiple elements. The array is not exclusively used in the vector. It is useful whenever you need multiple (possibly many) variables.

The following program defines an integer array with 3 elements and assigns a value to each element. Equivalently, you can see it as a vector with 3 elements.

You can see the essential feature of an array in this example.

  • An array holds a sequence of the same type of elements.
  • The declaration of an array is the same as a single variable except with the size of the array in () .
  • You can access each element by a subscript (i.e., index). The subscript is an integer number, starting from 1, and it is enclosed with () .
  • Each element can be treated as a single variable.
  • The array elements are operated collectively in print .

The subscript 3 in the above example means the number of elements and the last subscript (index) because the subscript starts from 1. You may feel uncomfortable of this system if you like C or Python which has the 0-origin subscript. Fortran is a language of 1-origin subscript, and it is useful to describe mathematical algorithms.

When an array is declared, its elements are not defined. So, we do not expect that a just-declared array has particular numbers. It is the same as a variable, and the programmer is responsible for assigning some values to the array before using it.

Contrary to R or Python, in Fortran, the array holds only a particular type of data declared in the program. In the above example, integer::vec(3) can contain only integer numbers. With this limitation, Fortran can efficiently manipulate the arrays.

It is still possible to develop a system in Fortran to hold the various data types in a single array in Fortran (like data.frame in R) if you develop it from scratch (or look for existing programs to do this). Or, you just avoid to deal with such a flexible array at a time, look for a workaround to use simple arrays. For example, if you should hold both characters and numbers, you can replace all the characters with sequential integer code, and all the information is represented as numbers.

Another way of declaration

There is another way to declare an array: the dimension attribute. The following piece of program is equivalent to integer::vec(3) .

In my personal opinion, this style is less intuitive than vec(3) to declare a 3-element array. I will not use dimension in the following sections.

Arbitrary subscript

You can use any range of subscript in the array. The range should be declared as (stard index:end index) in the declaration statement. You can not use real numbers a subscript in the array. For example, the following program supports the range \(-1\) to \(2\) for the array.

Using this feature, you can create a 0-origin subscript easily. Nevertheless, you should not abuse this feature because of a lack of readability.

Array section (sub-array)

You can extract a section of an array and use it as a separate array, like a section of characters. The following program shows only the 2nd and the 3rd elements.

The sub-array is defined with a range, (start index:end index) , instead of a single subscript. In the above case, it shows only the elements 2 and 3. The index system is very similar to for characters.

Assignment to array

Literal arrays.

There are several options to assign literal values to an array. The easiest way is to put a single value to each element, as shown in the previous example. Another way assigns multiple literal-values to the array at the same time. The following program has the same results as the previous one.

The multiple literals are encircled with [ and ] , and its number of elements should be the same as the array variable. Similar to a single variable, the initialization can be done in the declaration statement.

When assigning multiple-literals, you have to check the size of the data. For example, the following statement fails.

The brackets [ and ] to define an array was introduced in Fortran 2003. Traditionally, a combination of a slash symbol and parentheses, (/ and /) , was used (and is still used). The above code looks like this using the traditional symbols.

Literal scalar

When you assign a scalar to an array, all elements of the array are the scalar value. It is useful to initialize an array with a particular value.

Substitution

If you have two arrays with the same size, you can directly substitute one to another.

If the arrays have different sizes, the compiler may fail (or, it passes, but the executable may fail in run time).

Reading from keyboard

The multiple values can also be from the keyboard with the read statement. It reads multiple values at the same time as multiple variables are specified.

Unknown size of an array

In many cases, the size of the vector (array) is unknown at the beginning of the program, but it will be determined in the program. Fortran has an allocatable array which size is not defined in the declaration statement but fixed in the program. The following example is for an allocatable array.

You can see the usage of allocatable arrays.

  • The declaration of allocatable arrays is the same as the fixed-length array except for allocatable with the range as (:) .
  • To fix the size, use the allocatable statement as allocate(variable(size)) .
  • The allocated array can be used in the same way as a fixed-length array.
  • After using the allocated array, the deallocate statement releases the storage of the array. It is not needed if you do not resize this array with a different size.

Precisely, allocate reserves the storage in memory and assign it to the array. If the memory requirement is too large, the program stops with an error. If successful, the array is tied with the allocated memory with the specified size. The deallocate statement releases the allocated memory, and the status of an array is back to non-allocated . If you want to resize the array, you have to deallocate it once, then call allocate with the new size (it was relaxed in Fortran 2003, see the later chapter). When the program stops, regardless of normal end or error stop, all the allocated arrays will be automatically deallocated, so you should not be concerned about the arrays.

The allocated array initially has nonsense values i.e., uninitialized. The initialization of the array is the responsibility of the programmer.

There is an intrinsic function to test if an allocatable array has already allocated or not.

  • An array is a collective object of variables.
  • An array is defined with the variable name followed by the range of subscripts (start:end) in the declaration statement.
  • An arbitrary range of subscripts can be defined.
  • Multiple values can be assigned to an array using [ ] , or equivalently (/ /) .
  • An array-section is available as the variable name followed by (start:end) .
  • When the size of the array is unknown, an allocatable array should be declared with allocatable and (:) in the declaration. Use allocate to define the size and deallocate to release the storage.
  • An intrinsic function, allocated() , returns .true. if the allocatable array is allocated.
  • Define integer,parameter::n=5 and use it to specify the size of an array. See if it works when the definition of n is changed to integer::n=5 is used.
  • Define a real array with 5 elements, read the values from a keyboard, swap between 1st and the 3rd elements, and between 2nd and 4th elements, then print all the elements.
  • Create a program to read an integer \(n\) , allocate an array with \(n\) elements, read the values to this array, and print the values.

Array operations

Array arithmetic.

One of the advantages of using arrays is to make array operations much more straightforward than a single scalar variable. Primarily if you treat an array as a vector, the vector operation is simply written like a mathematical formula. The following program calculates a sum of two vectors (i.e.  \(\mathbf{c}=\mathbf{a}+\mathbf{b}\) ).

The arithmetic operators such as + , - , * , / , and ** perform element-by-element operations for arrays. Note that * does not mean the matrix multiplication but the element-by-element multiplication.

When the operation involves both array and scalar, the scalar will be broadcasted to all the elements in the array. See the following example.

By the way, the above code may secretly allocate a temporary array to store the result of a+5 as a single variable. In the current computers, it should not be a performance bottleneck.

Elemental functions

The element-wise operation is also applied in mathematical functions. For example, sin(x) for the array x returns the array with the sine of each element in x . The size of the return value is the array with the same size as input.

Such elemental functions include sin , cos , tan , log , exp , abs , sqrt , and so on.

Logical array

As the arithmetic operations and elemental functions, the logical operations can be applied to an array, and the result is also the array with the same size as input. The following example shows that the resulting array can be logical and stored in an array.

Array functions

Fortran has many functions to summarize or to inquire about an array. A typical function is sum(a) which computes the summation of elements in the array a . The function size(a) returns the size (the number of elements) of an array. The following table shows some useful functions.

Most of the above functions can accept a mask vector, as a second argument with a mask= identifier, defining which elements are involved in the computation. The mask vector is logical, and it has the same size as the input vector. If an element of the mask is .true. , the corresponding input is used, and if .false. , the corresponding input is ignored.

For example, the following example computes the sum of an array using only positive numbers.

  • Basic arithmetics and mathematical functions can be applied to each element.
  • The multiplication between arrays ( * ) performs element-by-element.
  • A scalar will be broadcasted to all elements through arithmetic operations.
  • Many array functions are available.
  • See what happens when incompatible sized arrays are involved in an arithmetic operation.
  • Read 5-element integer array and compute the average of the values.
  • Modify the above program to compute the average only from positive input values.
  • Compute the standard deviation of a given real array.
  • Compute the figure-skating average of a real array. This average is calculated from the data in which the highest and the lowest values have been discarded.

Multi-dimensional array

2-dimensional array.

Fortran can take care of higher dimensional arrays. The 2-dimensional array is often used as a matrix (and the 1-dimensional array is for a vector). Fortran is designed to efficiently manipulate the arrays (i.e., vectors and matrices). Here we deal only with the 2-dimensional array. See the other textbooks for details of higher-order arrays.

The higher-dimensional array has the same usage as the 1-dimensional array in deceleration, array functions, subarrays, allocation, and so forth. The only difference is the subscript (index) to specify the element. We will look at the basic features of a 2-dimensional array in the next sections.

Decrelation

The following code defines a \(2 \times 2\) array (matrix), and each of4 elements has a value.

The above array defines the following matrix. \[ \left[ \begin{array}{cc} 10&20 \\ 30&40 \end{array} \right] \] The usage of 2-dimensional array is similar to a vector except for subscripts (indices). The matrix has 2 indices in () separated by a comma. The first index is for ow, the second is for column same as the mathematical notation like \(A_{ij}\) where \(i\) is for row and \(j\) is for a column.

  • The declaration of the array is the same as a single variable with the size of the array in () as (row, column) .

The multi-dimensional array can be defined with dimension . We will not use this method in this tutorial.

Order in memory

In the previous example, you may notice that the order of elements in output is not intuitive. A computer should rearrange the multi-dimensional data into a 1-dimensional structure in memory. In Fortran, the 2-dimensional array is treated as a collection of column vectors (so-called the column-major storage).

\[ \left[ \begin{array}{c|c} 10&20 \\ 30&40 \end{array} \right] \rightarrow \left[ \begin{array}{c} 10\\ 30\\ \hline 20\\ 40 \end{array} \right] \]

This rearrangement (column-major) is reasonable in mathematics; R is also using this order. However, it is different from C and some other languages (row-major), and when you come from such languages, please be careful about the array rearrangement in memory.

The order becomes an issue when you read a matrix from the keyboard.

If you type 1 2 3 4 in terminal, the matrix will be as follows. \[ \left[ \begin{array}{cc} 1&3 \\ 2&4 \end{array} \right] \]

If you want to access the matrix along with rows, you have to write loops (see exercises).

Inquery functions for array shape

Fortran has several functions to get the size of an array.

For example, using the \(2 \times 3\) matrix, the above functions return the following values.

  • size(a) returns 6 (scalar).
  • shape(a) returns [2,3] (vector).

The size function can get the number of elements in a row (or column) as scalar using the 2nd parameter.

The assignment of literal values to a 2-dimensional array (hereafter a matrix) is not straightforward because we have to consider the dimension. There are several ways to assign values to a matrix. In the following example, we try to have the following \(2 \times 2\) matrix.

\[ \left[ \begin{array}{cc} 10&20 \\ 30&40 \end{array} \right] \]

The Fortran program shows 3 different ways to do it.

The first method substitute rows to the matrix. The second is the same way as first but in a column-wise manner. The third uses the reshape function, which rearranges a vector to fit the matrix with a given shape. Using reshape , you should consider the column-major order of elements to be aligned in the matrix.

The 3rd method is the same as reshape([10,30,20,40],[2,2]) instead of using shape(mat) .

Other values

You can assign a scalar to an array so that all the elements have the same value. It is useful to initialize an array. You can also assign a (sub)array to another array.

Computations

Element-by-element operations.

As 1-dimensional arrays, higher-dimensional arrays can be involved in simple arithmetic such as + and * . Also, most of the mathematical functions like log() can be applied to all elements at the same time.

Matrix multiplication and manipulation

In Fortran, the function for matrix multiplication matmul is available by default. It accepts 2 arrays, and compute the product, and return the result as a matrix. If the size of matrices is unconformable, the compilation fails, or the program fails in run time (or returns wrong results). See the following example.

The function transpose gives you a transposed matrix.

You can apply array functions to a particular dimension of an array using the second argument. For example, given a(2,2) , you can compute the sum of elements by column (along rows) as sum(a,1) . With this, the function returns a vector containing all results for rows (or columns).

  • A 1-dimensional array represents a vector, and 2-dimensional array, a matrix.
  • A 2-dimensional array (matrix) is a collection of 1-dimensional arrays (vectors).
  • To access an element of a 2-dimensional array, use 2 indices in () ; row the first and column the second.
  • For a matrix, you can use the same arithmetics rules and functions as a vector.
  • Use matmul() for matrix multiplication; * performs element-by-element operations.
  • The transposed matrix is available by transpose() .
  • An array function can be applied to a particular dimension (i.e., row-wise or column-wise).

See the above code.

  • What does b have? Why do we need b=0 ?
  • What does c have? How do you compute the square root of each element in c ?
  • What will be shown?
  • Write a program to show a matrix in row-wise (as in mathematical formula).
  • Getting started with Fortran
  • Advanced array sections: subscript triplets and vector subscripts
  • Allocatable arrays
  • Array constructors
  • Array nature specification: rank and shape
  • Array operations
  • Basic notation
  • Whole arrays, array elements and array sections
  • C interoperability
  • Execution Control
  • Explicit and implicit interfaces
  • Intrinsic procedures
  • Modern alternatives to historical features
  • Object Oriented Programming
  • Procedures - Functions and Subroutines
  • Program units and file layout
  • Source file extensions (.f, .f90, .f95, ...) and how they are related to the compiler.
  • Usage of Modules

Fortran Arrays Whole arrays, array elements and array sections

Fastest entity framework extensions.

Consider the array declared as

Then we have three aspects of interest:

  • The whole array x ;
  • Array elements, like x(1) ;
  • Array sections, like x(2:6) .

Whole arrays

In most cases the whole array x refers to all of the elements of the array as a single entity. It may appear in executable statements such as print *, SUM(x) , print *, SIZE(x) or x=1 .

A whole array may reference arrays which aren't explicitly shaped (such as x above):

An assumed-size array may also appear as a whole array, but in limited circumstances only (to be documented elsewhere).

Array elements

An array element is referred to be giving integer indexes, one for each rank of the array, denoting the location in the whole array:

An array element is a scalar.

Array sections

An array section is a reference to a number of elements (perhaps just one) of a whole array, using a syntax involving colons:

The final form above uses a vector subscript . This is subject to a number of restrictions beyond other array sections.

Each array section is itself an array, even when just one element is referenced. That is x(1:1,1) is an array of rank 1 and x(1:1,1:1) is an array of rank 2.

Array sections do not in general have an attribute of the whole array. In particular, where

the assignment

is not allowed: x(:) , although an array section with all elements of x , is not an allocatable array.

is fine when x is of the shape of the right-hand side.

Array components of arrays

We may also refer to whole arrays, array elements and array sections in more complicated settings.

From the above, x is a whole array. We also have

In such cases we are not allowed to have more than one part of the reference consisting of an array of rank 1. The following, for example, are not allowed

Description

The value can be a constant or the result of an expression. The kinds of assignment statements: are arithmetic, logical, character, and record assignments.

Arithmetic Assignment

v is of numeric type and is the name of a variable, array element, or record field.

e is an arithmetic expression, a character constant, or a logical expression. Assigning logicals to numerics is nonstandard, and may not be portable; the resultant data type is, of course, the data type of v . @

Compiling with any of the options -i2 , -dbl , -r8 , or -xtypemap can alter the default data size of variables and expressions. This is discussed in Chapter 2. See also the Fortran User's Guide for a description of these options.

Example: An assignment statement:        REAL A, B        DOUBLE PRECISION V        V = A * B

The above code is compiled exactly as if it were the following:        REAL A, B        DOUBLE PRECISION V        V = DBLE( A * B )

Logical Assignment

v is the name of a variable, array element, or record field of type logical.

e is a logical expression, or an integer between -128 and 127, or a single character constant.

Execution of a logical assignment statement causes evaluation of the logical expression e and assignment of the resulting value to v . If e is a logical expression (rather than an integer between -128 and 127, or a single character constant), then e must have a value of either true or false.

Logical expressions of any size can be assigned to logical variables of any size. The section on the LOGICAL statement provides more details on the size of logical variables.

Character Assignment

The constant can be a Hollerith constant or a string of characters delimited by apostrophes (') or quotes ("). The character string cannot include the control characters Control-A, Control-B, or Control-C; that is, you cannot hold down the Control key and press the A, B, or C keys. If you need those control characters, use the char() function.

If you use quotes to delimit a character constant, then you cannot compile with the -xl option, because, in that case, a quote introduces an octal constant. The characters are transferred to the variables without any conversion of data, and may not be portable.

Character expressions which include the // operator can be assigned only to items of type CHARACTER . Here, the v is the name of a variable, substring, array element, or record field of type CHARACTER ; e is a character expression.

Execution of a character assignment statement causes evaluation of the character expression and assignment of the resulting value to v . If the length of e is more than that of v , characters on the right are truncated. If the length of e is less than that of v , blank characters are padded on the right.

Record Assignment

v and e are each a record or record field. @

The e and v must have the same structure. They have the same structure if any of the following occur:

Both e and v are fields with the same elementary data type.

Both e and v are records with the same number of fields such that corresponding fields are the same elementary data type.

Both e and v are records with the same number of fields such that corresponding fields are substructures with the same structure as defined in 2, above.

The sections on the RECORD and STRUCTURE statements have more details on the structure of records.

Example 1: Arithmetic assignment:        INTEGER I2*2, J2*2, I4*4        REAL R1, QP*16 ! ( REAL*16 is SPARC only )        DOUBLE PRECISION DP        COMPLEX C8, C16*16, QC*32 ! (COMPLEX*32 is SPARC only)        J2 = 29002        I2 = J2        I4 = (I2 * 2) + 1        DP = 6.4D9        QP = 6.4Q9        R1 = DP        C8 = R1        C8 = ( 3.0, 5.0 )        I2 = C8        C16 = C8        C32 = C8

Example 2: Logical assignment:        LOGICAL B1*1, B2*1        LOGICAL L3, L4        L4 = .TRUE.        B1 = L4        B2 = B1

Example 3: Hollerith assignment:        CHARACTER S*4        INTEGER I2*2, I4*4        REAL R        S = 4Hwxyz        I2 = 2Hyz        I4 = 4Hwxyz        R = 4Hwxyz

Example 4: Character assignment:        CHARACTER BELL*1, C2*2, C3*3, C5*5, C6*6        REAL Z        C2 = 'z'        C3 = 'uvwxyz'        C5 = 'vwxyz'        C5(1:2) = 'AB'        C6 = C5 // C2        BELL = CHAR(7)     Control Character (^G)

Example 5: Record assignment and record field assignment:        STRUCTURE /PRODUCT/               INTEGER*4 ID               CHARACTER*16 NAME               CHARACTER*8 MODEL               REAL*4 COST               REAL*4 PRICE        END STRUCTURE        RECORD /PRODUCT/ CURRENT, PRIOR, NEXT, LINE(10)        ...        CURRENT = NEXT Record to record        LINE(1) = CURRENT Record to array element        WRITE ( 9 ) CURRENT Write whole record        NEXT.ID = 82 Assign a value to a field

  • © 2010, Oracle Corporation and/or its affiliates

IMAGES

  1. Fortran Programming Tutorials (Revised) : 037 : Fortran Array Indexing, Printing arrays

    fortran array assignment

  2. The storage format of arrays in Fortran (a), and the plastic strain

    fortran array assignment

  3. Fortran Programming Tutorials (Revised) : 040 : Modules + Array Valued Functions

    fortran array assignment

  4. PPT

    fortran array assignment

  5. The Fortran Language

    fortran array assignment

  6. Fortran

    fortran array assignment

VIDEO

  1. Lecture 6

  2. SGD 113 Array Assignment

  3. Lecture 7

  4. Find Minimum and Maximum number using array in Fortran||Array in Fortran|Fortran IDE #fortran #array

  5. Array Assignment

  6. Bank Array Assignment

COMMENTS

  1. Arrays

    Arrays are a central object in Fortran. The creation of dynamic sized arrays is discussed in the allocatable arrays section. To pass arrays to procedures four ways are available. The preferred way to pass arrays to procedures is as assumed-shape arrays. Higher-dimensional arrays can be passed in a similar way.

  2. Fortran: initializing of and assigning value to arrays

    Fortran array assignment with two indices. 1. Array assignment erases previous values on array. 1. Assignment of allocatable array with itself values. Hot Network Questions What does "those mythical relations started needing her to cure their mythical ailments in far-off places" mean?

  3. Fortran

    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.

  4. PDF Arrays in Fortran

    Fortran uses array notation for whole arrays and subarrays. Copy whole array: real*8, dimension(10) :: x,y x = y And much more. COE 322 - 2022 — - 6. 6. Array sections example ... • in a single array assignment statement by using sections. Initialize the array x with values that allow you to check the correctness of your code. COE 322 ...

  5. Fortran Array Data and Arguments and Vectorization

    Arrays A and B are accessed indirectly through the index array 'ind'. The Fortran array notation semantics requires that the result of executing this assignment statement be equivalent to evaluating the right hand side for all array elements and assigning the result to the target.

  6. Getting started with Fortran

    Arrays in Fortran Fortran arrays are very powerful and allows to define: matrices; vectors; other arrays with up to 7 dimensions. Arrays Syntax & arrays sections Arrays syntax In older Fortran codes, arrays are usually accessed element by element while in modern Fortran, what is called the Fortran 90 array syntax is used.

  7. Fortran 77 Tutorial

    The data type Fortran uses for representing such objects is the array . A one-dimensional array corresponds to a vector, while a two-dimensional array corresponds to a matrix. To fully understand how this works in Fortran 77, you will have to know not only the syntax for usage, but also how these objects are stored in memory in Fortran 77.

  8. Array and Matrix • Fortran Tutorial

    Array and Matrix. Program : Array. OUTER_LOOP : DO ii = 1, 10. vec(ii) = ii. There is intrinsic SIZE function to determine the length of a vector, SIZE (vec). Another example where we use implied DO loop to assign an array: vec = [(ii, ii = 1, 10)] Dynamic array allocation with ALLOCATABLE:

  9. More on arrays

    Assignment, again; Reallocation of arrays; Array of character variable; Some algorithms using arrays; Back to index.html. More on arrays Basic usage of arrays Fundamental data-structure in Fortran. An array is a collection of elements (or objects), which have the same data type. The array is the fundamental data-structure that Fortran can ...

  10. Arrays

    Contrary to R or Python, in Fortran, the array holds only a particular type of data declared in the program. ... Assignment to array Literals. The assignment of literal values to a 2-dimensional array (hereafter a matrix) is not straightforward because we have to consider the dimension. There are several ways to assign values to a matrix.

  11. Fortran Tutorial => Whole arrays, array elements and array sections

    Intrinsic assignment for the whole array, invoking allocation end function An assumed-size array may also appear as a whole array, but in limited circumstances only (to be documented elsewhere). Array elements. An array element is referred to be giving integer indexes, one for each rank of the array, denoting the location in the whole array:

  12. PDF Fortran 90 ArraysFortran 90 Arrays

    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).

  13. Array Assignment Statements

    Array Assignment Statements. Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference. Download PDF. ID767251. Date9/08/2022. Version. 2024.1 (latest) 2024.0 2023.2 2023.1 2023.0. Public.

  14. Assignment (FORTRAN 77 Language Reference)

    Logical Assignment. v is the name of a variable, array element, or record field of type logical.. e is a logical expression, or an integer between -128 and 127, or a single character constant.. Execution of a logical assignment statement causes evaluation of the logical expression e and assignment of the resulting value to v.If e is a logical expression (rather than an integer between -128 and ...

  15. Different shape for array assignment in Fortran

    produces a rank-1 array containing the specified elements of V_cvo_temp, it essentially flattens the array into a vector and loses their locations along the way. This is why the first expression returns a rank-1 array with 1 element - it's the location of an element in a rank-1 array. The problem with this solution. V_Min=minloc(abs(V_cvo(2:5,1 ...