Declaring One Dimensional Arrays in Fortran

Fortran, one of the oldest and most widely used programming languages in scientific computing, provides robust support for arrays, which are essential for storing multiple values of the same type in a single data structure. Arrays simplify the handling of large sets of data and are crucial for tasks like numerical simulations, engineering calculations, and scientific data processing.

Among arrays, one-dimensional arrays are the simplest type. They are essentially lists of elements that can be accessed using a single index. This post provides a comprehensive discussion on declaring and using one-dimensional arrays in Fortran, including syntax, initialization, accessing elements, best practices, and practical examples.

1. Introduction to One-Dimensional Arrays

A one-dimensional (1D) array is a linear collection of elements of the same data type. Each element can be uniquely identified using an index.

Key points about 1D arrays in Fortran:

  • All elements must have the same data type (integer, real, double precision, logical, character, etc.)
  • Array indices in Fortran start from 1 by default
  • Arrays can be initialized at declaration or assigned values later
  • 1D arrays simplify operations like summation, iteration, and element-wise computation

2. Syntax for Declaring One-Dimensional Arrays

The general syntax for declaring a one-dimensional array in Fortran is:

type :: array_name(array_size)
  • type is the data type of the array elements (integer, real, double precision, logical, character)
  • array_name is the identifier for the array
  • array_size is the number of elements in the array

2.1 Examples

real :: arr(5)       ! Real array with 5 elements
integer :: numbers(10) ! Integer array with 10 elements
logical :: flags(4)  ! Logical array with 4 elements
character(len=20) :: names(3) ! Character array with 3 elements

Explanation:

  • arr can hold 5 real numbers
  • numbers can hold 10 integers
  • flags can hold 4 logical values
  • names can hold 3 strings of up to 20 characters each

3. Initializing One-Dimensional Arrays

Arrays can be initialized at declaration using array constructors:

integer :: numbers(5) = (/1, 2, 3, 4, 5/)
real :: temperatures(4) = (/36.5, 37.2, 38.0, 36.8/)

Explanation:

  • The array constructor (/ ... /) lists the initial values
  • Ensures all elements are initialized and avoids undefined values

3.1 Assigning Values After Declaration

You can also assign values to array elements individually or as a group:

integer :: numbers(5)
numbers(1) = 10
numbers(2) = 20
numbers(3) = 30
numbers(4) = 40
numbers(5) = 50

Or using an array constructor:

numbers = (/10, 20, 30, 40, 50/)

Explanation:

  • Array elements can be updated individually
  • Using array constructor is more concise and readable

4. Accessing Array Elements

Array elements are accessed using parentheses and an index:

print *, numbers(1)  ! Prints first element
print *, numbers(5)  ! Prints last element

Explanation:

  • Indexing starts at 1 by default
  • You can read, update, or perform calculations using individual elements

4.1 Iterating Over a One-Dimensional Array

Using a loop to access all elements:

integer :: i
do i = 1, 10
print *, "numbers(", i, ") =", numbers(i)
end do

Explanation:

  • do loop iterates from 1 to array size
  • Prints all elements in sequence

4.2 Array Length Function

Fortran provides the size() function to get the length of an array:

print *, "Length of numbers array:", size(numbers)

Explanation:

  • Avoids hardcoding array size
  • Useful for loops and dynamic calculations

5. Operations on One-Dimensional Arrays

5.1 Summation of Elements

integer :: sum
sum = 0
do i = 1, size(numbers)
sum = sum + numbers(i)
end do print *, "Sum of elements:", sum

Explanation:

  • Loop iterates over array elements
  • Computes cumulative sum
  • 1D arrays simplify such aggregate operations

5.2 Maximum and Minimum

integer :: max_val, min_val
max_val = numbers(1)
min_val = numbers(1)

do i = 2, size(numbers)
if (numbers(i) > max_val) max_val = numbers(i)
if (numbers(i) < min_val) min_val = numbers(i)
end do print *, "Maximum:", max_val print *, "Minimum:", min_val

Explanation:

  • Iterates over array to find largest and smallest element
  • Useful in data analysis and scientific computations

5.3 Element-wise Operations

integer :: squares(10)
do i = 1, 10
squares(i) = numbers(i) ** 2
end do print *, "Squares:", squares

Explanation:

  • Computes square of each element
  • Demonstrates element-wise arithmetic using loops

6. One-Dimensional Arrays with Real Numbers

1D arrays are commonly used with real numbers for measurements, simulation data, or scientific calculations:

real :: temperatures(5)
temperatures = (/36.5, 37.0, 38.2, 36.8, 37.5/)

do i = 1, size(temperatures)
print *, "Temperature(", i, ") =", temperatures(i)
end do

Explanation:

  • Stores multiple temperature readings
  • Loops iterate and print all values

6.1 Calculating Average

real :: sum_temp, avg_temp
sum_temp = 0.0

do i = 1, size(temperatures)
sum_temp = sum_temp + temperatures(i)
end do avg_temp = sum_temp / size(temperatures) print *, "Average temperature:", avg_temp

Explanation:

  • Computes sum of array elements
  • Divides by number of elements to get the average

7. Logical and Character Arrays

7.1 Logical Array Example

logical :: flags(5)
flags = (/ .true., .false., .true., .true., .false. /)

do i = 1, size(flags)
print *, "flags(", i, ") =", flags(i)
end do

Explanation:

  • Stores multiple logical values
  • Useful in conditional checks for multiple elements

7.2 Character Array Example

character(len=10) :: names(3)
names = (/ "Alice", "Bob", "Charlie" /)

do i = 1, size(names)
print *, "names(", i, ") =", trim(names(i))
end do

Explanation:

  • Stores multiple strings
  • trim() removes trailing spaces for clean output

8. Arrays with Implicit Loops (Fortran 90+)

Fortran supports array operations without explicit loops:

integer :: numbers(5), squares(5)
numbers = (/1,2,3,4,5/)
squares = numbers ** 2
print *, "Squares:", squares

Explanation:

  • Computes squares of all elements simultaneously
  • Modern Fortran allows vectorized operations, improving performance and readability

9. Best Practices for One-Dimensional Arrays

  1. Initialize arrays whenever possible to avoid undefined values.
  2. Use size() instead of hardcoding array length in loops.
  3. Choose appropriate data types for the array (integer, real, logical, character).
  4. Use array constructors for concise initialization.
  5. Trim character arrays for clean output.
  6. Prefer array operations over explicit loops when possible for efficiency.

10. Practical Examples

10.1 Sum and Average of Integer Array

integer :: numbers(10), sum, avg
numbers = (/1,2,3,4,5,6,7,8,9,10/)
sum = 0

do i = 1, size(numbers)
sum = sum + numbers(i)
end do avg = sum / size(numbers) print *, "Sum:", sum print *, "Average:", avg

Explanation:

  • Demonstrates initialization, iteration, summation, and averaging
  • Illustrates a common pattern in scientific and engineering computations

10.2 Finding Maximum in Real Array

real :: temperatures(5), max_temp
temperatures = (/36.5, 37.0, 38.2, 36.8, 37.5/)
max_temp = temperatures(1)

do i = 2, size(temperatures)
if (temperatures(i) > max_temp) max_temp = temperatures(i)
end do print *, "Maximum temperature:", max_temp

Explanation:

  • Finds maximum value efficiently using a loop

10.3 Logical Array Check

logical :: flags(5)
flags = (/ .true., .false., .true., .false., .true. /)

do i = 1, size(flags)
if (flags(i)) then
    print *, "flags(", i, ") is TRUE"
else
    print *, "flags(", i, ") is FALSE"
end if
end do

Explanation:

  • Demonstrates conditional checks on logical array elements

10.4 Character Array Processing

character(len=20) :: names(3)
names = (/ "Alice", "Bob", "Charlie" /)

do i = 1, size(names)
print *, "Welcome,", trim(names(i))
end do

Explanation:

  • Iterates over character array
  • Outputs formatted greeting

11. Summary

  • One-dimensional arrays in Fortran are linear collections of elements of the same type.
  • They simplify the management of multiple values in programs.
  • Arrays can be initialized at declaration or assigned values later.
  • Elements are accessed using indices starting from 1.
  • Loops, conditional statements, and array operations make working with arrays efficient.
  • Logical and character arrays allow storing flags and strings for program logic.
  • Modern Fortran allows array arithmetic without explicit loops for concise and efficient code.
  • Best practices include initializing arrays, using size() for loops, and using array constructors for readability.

12. Complete Example: One-Dimensional Arrays in Action

program array_demo
implicit none
integer :: numbers(10)
integer :: i, sum, max_val
real :: temperatures(5), avg_temp
logical :: flags(5)
character(len=10) :: names(3)
! Integer array initialization
numbers = (/1,2,3,4,5,6,7,8,9,10/)
sum = 0
do i = 1, size(numbers)
    sum = sum + numbers(i)
end do
print *, "Sum of numbers:", sum
! Maximum of integer array
max_val = numbers(1)
do i = 2, size(numbers)
    if (numbers(i) > max_val) max_val = numbers(i)
end do
print *, "Maximum number:", max_val
! Real array initialization
temperatures = (/36.5, 37.0, 38.2, 36.8, 37.5/)
avg_temp = sum(temperatures)/size(temperatures)
print *, "Average temperature:", avg_temp
! Logical array
flags = (/ .true., .false., .true., .false., .true. /)
do i = 1, size(flags)
    print *, "Flag(", i, ") =", flags(i)
end do
! Character array
names = (/ "Alice", "Bob", "Charlie" /)
do i = 1, size(names)
    print *, "Hello,", trim(names(i))
end do
end program array_demo

Explanation:

  • Demonstrates integer, real, logical, and character arrays
  • Shows initialization, summation, maximum value, average, and iteration
  • Combines multiple practical use cases of one-dimensional arrays

Comments

Leave a Reply

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