Arrays are a fundamental concept in Fortran, allowing you to store and manipulate collections of data efficiently. This section will cover the basics of arrays, including their declaration, initialization, and basic operations.

Key Concepts

  1. Array Declaration: How to declare arrays in Fortran.
  2. Array Initialization: Different ways to initialize arrays.
  3. Accessing Array Elements: How to access and modify elements in an array.
  4. Array Operations: Basic operations that can be performed on arrays.

Array Declaration

In Fortran, arrays can be declared with a specific size or dynamically allocated. Here’s how you can declare arrays:

Static Arrays

Static arrays have a fixed size determined at compile time.

! Declaration of a 1-dimensional array with 5 elements
INTEGER :: array1D(5)

! Declaration of a 2-dimensional array with 3 rows and 4 columns
REAL :: array2D(3, 4)

Dynamic Arrays

Dynamic arrays can be allocated at runtime, allowing for more flexibility.

! Declaration of a dynamic array
INTEGER, ALLOCATABLE :: dynamicArray(:)

! Allocating the array with 10 elements
ALLOCATE(dynamicArray(10))

Array Initialization

Arrays can be initialized in several ways:

Inline Initialization

You can initialize arrays at the time of declaration.

! Inline initialization of a 1-dimensional array
INTEGER :: array1D(5) = (/ 1, 2, 3, 4, 5 /)

! Inline initialization of a 2-dimensional array
REAL :: array2D(2, 2) = RESHAPE((/ 1.0, 2.0, 3.0, 4.0 /), SHAPE(array2D))

Using Loops

You can also initialize arrays using loops.

INTEGER :: i
DO i = 1, 5
    array1D(i) = i * 2
END DO

Accessing Array Elements

Array elements can be accessed and modified using their indices.

! Accessing the first element of array1D
PRINT *, array1D(1)

! Modifying the second element of array1D
array1D(2) = 10

Multidimensional Arrays

For multidimensional arrays, you need to specify indices for each dimension.

! Accessing an element in a 2-dimensional array
PRINT *, array2D(1, 2)

! Modifying an element in a 2-dimensional array
array2D(2, 1) = 5.5

Array Operations

Fortran provides several intrinsic functions to perform operations on arrays.

Array Assignment

You can assign values to entire arrays or sections of arrays.

! Assigning a value to all elements of an array
array1D = 0

! Assigning values to a section of an array
array1D(2:4) = (/ 7, 8, 9 /)

Array Intrinsic Functions

Fortran has built-in functions to perform common operations on arrays.

! Finding the sum of all elements in an array
INTEGER :: sum
sum = SUM(array1D)

! Finding the maximum value in an array
INTEGER :: maxVal
maxVal = MAXVAL(array1D)

Practical Example

Here’s a complete example that demonstrates the concepts covered:

PROGRAM ArrayExample
    IMPLICIT NONE
    INTEGER :: i
    INTEGER, ALLOCATABLE :: dynamicArray(:)
    
    ! Allocate and initialize the dynamic array
    ALLOCATE(dynamicArray(10))
    DO i = 1, 10
        dynamicArray(i) = i * 3
    END DO
    
    ! Print the array elements
    PRINT *, "Array elements:"
    DO i = 1, 10
        PRINT *, dynamicArray(i)
    END DO
    
    ! Perform some operations
    PRINT *, "Sum of array elements:", SUM(dynamicArray)
    PRINT *, "Maximum value in array:", MAXVAL(dynamicArray)
    
    ! Deallocate the array
    DEALLOCATE(dynamicArray)
END PROGRAM ArrayExample

Exercises

Exercise 1: Array Initialization

  1. Declare a 1-dimensional array of size 7.
  2. Initialize the array with the first 7 prime numbers.
  3. Print the array elements.

Solution

PROGRAM PrimeArray
    IMPLICIT NONE
    INTEGER :: primes(7) = (/ 2, 3, 5, 7, 11, 13, 17 /)
    INTEGER :: i
    
    PRINT *, "Prime numbers:"
    DO i = 1, 7
        PRINT *, primes(i)
    END DO
END PROGRAM PrimeArray

Exercise 2: Array Operations

  1. Declare a 2-dimensional array of size 3x3.
  2. Initialize the array with values from 1 to 9.
  3. Calculate and print the sum of all elements in the array.

Solution

PROGRAM Sum2DArray
    IMPLICIT NONE
    INTEGER :: array2D(3, 3) = RESHAPE((/ 1, 2, 3, 4, 5, 6, 7, 8, 9 /), SHAPE(array2D))
    INTEGER :: sum
    
    sum = SUM(array2D)
    PRINT *, "Sum of 2D array elements:", sum
END PROGRAM Sum2DArray

Conclusion

In this section, you learned about the basics of arrays in Fortran, including their declaration, initialization, and basic operations. Arrays are a powerful tool for handling collections of data, and understanding them is crucial for effective Fortran programming. In the next section, we will delve into multidimensional arrays and more advanced array operations.

© Copyright 2024. All rights reserved