- 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
IMAGES
VIDEO
COMMENTS
FORTRAN 66 Interpretation of the EXTERNAL Statement Alternative Syntax for the PARAMETER Statement Alternative Syntax for Binary, Octal, and Hexadecimal Constants Alternative Syntax for a Record Specifier Alternative Syntax for the DELETE Statement …
Array Assignment Statements. Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference. Download PDF. ID 767251. Date 9/08/2022. Version. Public. View More. A newer version of this document is available. Customers should click here to go to …
This document provides examples to various array types in Fortran and their usage as local variables, function/subroutine parameters, as well as, examples to Fortran pointers and their usage. This document shows how various array data types and arguments are vectorized …
The slashes are fine for the 1-D array constructor, but gives syntax error for the assignment of data either for a row or column of a 2-D array (with XE 12.0.1.127). For example, [bash]!1-D parameter array integer, parameter:: fibonacci(10) = [1,1,2,3,5,8,13,21,34,55] !
For gfortran, Fortran 2003's assignment to an allocatable array was introduced in 4.6, 2011-01-28. As also commented, the command line option -fno-realloc-lhs 1 disables this automatic (re-)allocation, making the compiler not Fortran 2003+ compliant.
When you call the subroutine you are asserting (according to Fortran standard) there is no overlap. The compiler may make an intel_fast_memcpy substitution, including automatic decision at run time whether to use nontemporal stores.
The Intel Fortran page on the Array Assignment Statement suggests that isn't a problem because they do an array reverse using "A(1:20) = A(20:1:-1)", but I can't find any explicit rules one way or another.