Arrays are fundamental data structures in Fortran that allow programmers to store and manipulate multiple values of the same type efficiently. Accessing individual elements correctly is crucial for performing calculations, processing data, and implementing algorithms. This post provides a thorough explanation of accessing array elements in Fortran, including examples for one-dimensional (1D) and multi-dimensional arrays.
1. Introduction to Arrays
An array is a collection of elements of the same type, stored in contiguous memory locations. Arrays can be:
- One-dimensional (1D) – like a list
- Two-dimensional (2D) – like a matrix or table
- Multi-dimensional (3D or higher) – like tensors used in scientific computations
Accessing elements of an array requires specifying the index or indices corresponding to the element’s position.
2. One-Dimensional Arrays
A one-dimensional array can be thought of as a simple list of values. The syntax for declaring a 1D array is:
real :: arr(n)
Where n is the number of elements in the array.
Accessing Elements
Array elements are accessed using parentheses and an index:
array(index)
- Indexing in Fortran starts at 1 by default.
array(1)refers to the first element.array(n)refers to the last element.
Example: 1D Array
real :: arr(5)
arr = (/1.0, 2.0, 3.0, 4.0, 5.0/)
print *, "Third element:", arr(3)
Explanation:
- The array
arrcontains five elements. arr(3)accesses the third element.- Output:
Third element: 3.0
Modifying Elements
You can also modify individual elements:
arr(2) = 10.0
print *, "Modified second element:", arr(2)
- Changes the second element from 2.0 to 10.0.
- Output:
Modified second element: 10.0
Iterating Through 1D Arrays
Loops are commonly used to access and process each element:
integer :: i
do i = 1, 5
print *, "Element", i, "=", arr(i)
end do
Output:
Element 1 = 1.0
Element 2 = 10.0
Element 3 = 3.0
Element 4 = 4.0
Element 5 = 5.0
3. Two-Dimensional Arrays
A two-dimensional array, or matrix, is declared using two dimensions:
real :: matrix(rows, columns)
Accessing Elements
Elements are accessed using two indices:
matrix(i, j)
ispecifies the row number.jspecifies the column number.- Indexing starts at 1 by default.
Example: 2D Array
real :: matrix(3,3)
matrix = reshape((/1,2,3,4,5,6,7,8,9/), (/3,3/))
print *, "Element at (2,3):", matrix(2,3)
Explanation:
reshapearranges the list into a 3×3 matrix:
1 2 3
4 5 6
7 8 9
matrix(2,3)accesses the element in the second row and third column, which is6.- Output:
Element at (2,3): 6.0
Iterating Through 2D Arrays
Nested loops are used to access elements row by row:
integer :: i, j
do i = 1, 3
do j = 1, 3
print *, "Element at (", i, ",", j, ") =", matrix(i,j)
end do
end do
Output:
Element at (1,1) = 1.0
Element at (1,2) = 2.0
Element at (1,3) = 3.0
Element at (2,1) = 4.0
Element at (2,2) = 5.0
Element at (2,3) = 6.0
Element at (3,1) = 7.0
Element at (3,2) = 8.0
Element at (3,3) = 9.0
4. Multi-Dimensional Arrays
Fortran supports arrays with three or more dimensions, often used in physics simulations, weather modeling, or engineering computations.
real :: tensor(2,3,4)
tensor(i,j,k)accesses the element at rowi, columnj, depthk.
Example: 3D Array
real :: tensor(2,2,2)
tensor = reshape((/1,2,3,4,5,6,7,8/), (/2,2,2/))
print *, "Element at (2,1,2):", tensor(2,1,2)
- Accesses a specific element in a 3D structure.
5. Array Slicing and Subarrays
Fortran allows accessing subsets of arrays using slicing:
array(start:end)
Example: 1D Array Slice
real :: arr(5)
arr = (/1.0, 2.0, 3.0, 4.0, 5.0/)
print *, "Elements 2 to 4:", arr(2:4)
Output:
Elements 2 to 4: 2.0 3.0 4.0
Example: 2D Array Slice
real :: matrix(3,3)
matrix = reshape((/1,2,3,4,5,6,7,8,9/), (/3,3/))
print *, "Second row:", matrix(2,:)
print *, "Third column:", matrix(:,3)
Output:
Second row: 4.0 5.0 6.0
Third column: 3.0 6.0 9.0
:represents all elements along that dimension.
6. Modifying Array Elements
You can update individual elements or slices:
arr(1) = 10.0
matrix(3,2) = 12.0
matrix(:,1) = (/ 100.0, 200.0, 300.0 /)
- Updates both single elements and entire columns.
7. Accessing Elements in Loops
Loops combined with indexing are used for calculations over arrays:
Example: Sum of Elements in 1D Array
real :: sum
integer :: i
sum = 0.0
do i = 1, 5
sum = sum + arr(i)
end do
print *, "Sum of array elements:", sum
Example: Sum of Rows in 2D Array
integer :: i, j
real :: rowSum
do i = 1, 3
rowSum = 0.0
do j = 1, 3
rowSum = rowSum + matrix(i,j)
end do
print *, "Sum of row", i, "=", rowSum
end do
8. Using Functions to Access Array Elements
Fortran provides intrinsic functions for array operations:
size(array)– Returns number of elements in an arraylbound(array)– Returns lower bound indexubound(array)– Returns upper bound index
print *, "Array size:", size(arr)
print *, "Lower bound:", lbound(arr)
print *, "Upper bound:", ubound(arr)
- Useful for dynamic arrays and loops.
9. Advanced Access Patterns
9.1 Strided Access
print *, "Every second element:", arr(1:5:2)
- The format
start:end:stepallows skipping elements. - Output:
Every second element: 1.0 3.0 5.0
9.2 Reversing an Array
print *, "Reversed array:", arr(5:1:-1)
- Access elements in reverse order.
10. Real-World Applications
- Matrix Operations: Accessing elements for multiplication, addition, or determinant calculations.
- Data Analysis: Iterating through sensor readings or measurement arrays.
- Simulation Models: Using 3D arrays for physical grids in weather or physics simulations.
- Image Processing: Accessing pixel values stored in 2D arrays.
Example: Matrix Multiplication
real :: A(2,2), B(2,2), C(2,2)
integer :: i, j, k
A = reshape((/1,2,3,4/), (/2,2/))
B = reshape((/5,6,7,8/), (/2,2/))
C = 0.0
do i = 1, 2
do j = 1, 2
do k = 1, 2
C(i,j) = C(i,j) + A(i,k) * B(k,j)
end do
end do
end do
print *, "Matrix C:"
do i = 1, 2
print *, C(i,:)
end do
11. Best Practices
- Always use parentheses with indices –
array(index),matrix(i,j) - Check bounds – Avoid accessing elements outside declared ranges
- Use slicing for efficiency – When processing multiple elements
- Document multidimensional arrays – Clarify which index corresponds to rows
Leave a Reply