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
- Array Declaration: How to declare arrays in Fortran.
- Array Initialization: Different ways to initialize arrays.
- Accessing Array Elements: How to access and modify elements in an array.
- 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.
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
- Declare a 1-dimensional array of size 7.
- Initialize the array with the first 7 prime numbers.
- 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
- Declare a 2-dimensional array of size 3x3.
- Initialize the array with values from 1 to 9.
- 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.
Fortran Programming Course
Module 1: Introduction to Fortran
- Introduction to Fortran
- Setting Up the Development Environment
- Basic Syntax and Structure
- Writing Your First Fortran Program
Module 2: Basic Concepts
- Variables and Data Types
- Operators and Expressions
- Input and Output
- Control Structures: If Statements
- Control Structures: Loops
Module 3: Arrays and Strings
Module 4: Procedures and Functions
Module 5: Advanced Data Structures
Module 6: File Handling
Module 7: Advanced Topics
Module 8: Best Practices and Optimization
- Code Optimization Techniques
- Debugging and Profiling
- Writing Maintainable Code
- Fortran Standards and Portability