Array Assignment and Initialization in Fortran

Arrays are fundamental data structures in Fortran that allow you to store multiple values of the same type in a single variable. They are widely used in scientific computing, numerical analysis, simulations, and data processing.

Proper assignment and initialization of arrays is essential for reliable computations and clear code. Fortran provides several ways to assign values to arrays, including direct assignment, array constructors, and reshape for multi-dimensional arrays. This post explores these methods in detail.

1. Introduction to Arrays

An array in Fortran is a collection of elements of the same type, indexed by integers. Arrays can be one-dimensional (1D), two-dimensional (2D), or multi-dimensional.

Syntax for Declaring Arrays

real :: arr(5)        ! 1D array of size 5
integer :: matrix(3,4) ! 2D array with 3 rows and 4 columns
real :: cube(2,3,4)   ! 3D array
  • 1D array: Single index arr(i)
  • 2D array: Two indices matrix(i,j)
  • 3D or higher: Multiple indices for multi-dimensional arrays

2. Array Assignment Using Array Constructor

Fortran allows initializing arrays using array constructors, which are written with (/ ... /) for 1D arrays.

Example 1: 1D Array Assignment

program array_assignment_1D
real :: arr(5)
arr = (/1.0, 2.0, 3.0, 4.0, 5.0/)
print *, "Array:", arr
end program array_assignment_1D

Output:

Array: 1.0 2.0 3.0 4.0 5.0

Explanation:

  • Array arr is declared with 5 elements.
  • Array constructor (/ ... /) assigns values element-wise.
  • Ensures that all elements are explicitly initialized.

3. Partial Array Assignment

You can assign values to specific elements of an array using index notation.

Example 2: Assign Specific Elements

program partial_assignment
real :: arr(5)
arr = 0.0          ! Initialize all elements to 0
arr(2) = 10.0      ! Assign second element
arr(4) = 20.0      ! Assign fourth element
print *, "Array after partial assignment:", arr
end program partial_assignment

Output:

Array after partial assignment: 0.0 10.0 0.0 20.0 0.0

Explanation:

  • Initialize all elements to 0.0
  • Assign specific elements individually
  • Useful for sparse data or special initial conditions

4. Multi-Dimensional Array Initialization

Fortran provides the reshape function to initialize multi-dimensional arrays.

Syntax:

array = reshape((/ list_of_values /), shape=(/ dim1, dim2, ... /))

Example 3: 2D Array Using RESHAPE

program array_2D_reshape
real :: matrix(2,3)
matrix = reshape((/1.0, 2.0, 3.0, 4.0, 5.0, 6.0/), shape=(/2,3/))
print *, "2D Matrix:"
print *, matrix
end program array_2D_reshape

Output:

2D Matrix:
   1.0  3.0  5.0
   2.0  4.0  6.0

Explanation:

  • reshape fills the array in column-major order (Fortran stores arrays column-wise).
  • Shape specifies the dimensions of the array.

Example 4: 3D Array Using RESHAPE

program array_3D
real :: cube(2,2,2)
cube = reshape((/1,2,3,4,5,6,7,8/), shape=(/2,2,2/))
print *, "3D Cube Elements:"
print *, cube
end program array_3D

Explanation:

  • 3D array cube has dimensions 2x2x2
  • reshape organizes elements in column-major order
  • Useful for tensor computations or multi-dimensional grids

5. Using Array Constructors with Implied DO Loops

For large arrays, manually listing all elements can be tedious. You can use implied DO loops in array constructors.

Example 5: Implied DO for 1D Array

program implied_do_1D
integer :: arr(10)
arr = (/ (i*2, i = 1, 10) /)  ! Creates array 2,4,6,...,20
print *, "Array using implied DO:", arr
end program implied_do_1D

Output:

Array using implied DO: 2 4 6 8 10 12 14 16 18 20

Explanation:

  • (i*2, i = 1, 10) generates elements programmatically
  • Reduces manual entry and enhances scalability

Example 6: Implied DO for 2D Array

program implied_do_2D
integer :: matrix(3,3)
matrix = reshape((/ (i*10, i=1,9) /), shape=(/3,3/))
print *, "2D Matrix using implied DO:"
print *, matrix
end program implied_do_2D

Explanation:

  • (i*10, i=1,9) generates values 10,20,…,90
  • reshape converts 1D list to 3×3 matrix

6. Array Assignment with Whole Arrays

You can assign one array to another if they have the same size and type.

Example 7: Array-to-Array Assignment

program array_copy
integer :: arr1(5), arr2(5)
arr1 = (/1,2,3,4,5/)
arr2 = arr1
print *, "Copied Array:", arr2
end program array_copy

Explanation:

  • All elements are copied element-wise
  • Efficient for large arrays in numerical programs

7. Using Array Assignment with Mathematical Expressions

You can perform element-wise operations when assigning arrays.

Example 8: Element-wise Operations

program array_math
real :: arr(5), result(5)
arr = (/1.0, 2.0, 3.0, 4.0, 5.0/)
result = arr**2 + 1.0   ! Square each element and add 1
print *, "Resulting Array:", result
end program array_math

Output:

Resulting Array: 2.0 5.0 10.0 17.0 26.0

Explanation:

  • Operations are applied element-wise
  • Fortran supports arithmetic directly on arrays

8. Initializing Arrays with Constants

You can initialize arrays with a single repeated value using the repeat function (Fortran 90+) or assignment.

Example 9: Constant Array

program constant_array
integer :: arr(5)
arr = 7  ! All elements assigned 7
print *, "Array with constant value:", arr
end program constant_array

Output:

Array with constant value: 7 7 7 7 7

Explanation:

  • Simplifies initialization when all elements start with the same value

9. Advanced Multi-Dimensional Initialization

For larger grids, combining reshape, implied DO loops, and arithmetic expressions allows efficient array setup.

Example 10: 2D Grid Initialization

program grid_initialization
integer :: i, j
integer :: grid(3,3)
grid = reshape((/ (i + j, i=1,3, j=0,2) /), shape=(/3,3/))
print *, "2D Grid Elements:"
print *, grid
end program grid_initialization

Explanation:

  • Elements generated programmatically with implied DO
  • Useful for simulation grids, mesh generation, or matrix operations

10. Best Practices

  1. Always initialize arrays to avoid undefined behavior.
  2. Use array constructors or implied DO loops for readability.
  3. Use reshape for multi-dimensional arrays.
  4. Prefer array operations over element-wise loops for clarity and efficiency.
  5. Keep dimensions consistent when assigning one array to another.
  6. Comment complex initializations for maintainability.
  7. Use constants for repeated values to simplify code.

11. Practical Applications

  • Numerical simulations – Initializing grids, matrices, or vectors
  • Data processing – Preloading arrays with known values
  • Scientific computations – Element-wise operations and matrix setup
  • Control algorithms – Flags or state arrays
  • Graphical applications – Image pixel arrays or 2D/3D data

12. Comprehensive Example

program comprehensive_array
integer :: i, j
real :: vector(5), matrix(2,3)
! 1D array initialization
vector = (/1.0, 2.0, 3.0, 4.0, 5.0/)
print *, "Vector:", vector
! Multi-dimensional array using reshape
matrix = reshape((/1.0,2.0,3.0,4.0,5.0,6.0/), shape=(/2,3/))
print *, "Matrix</code></pre>

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *