Menu Close

Array Indexing – FORTRAN

Array indexing refers to the process of accessing individual elements of an array using their indices.

Array indices in Fortran start from 1 and can be used to retrieve or modify the values stored in specific locations within the array.

Example

To access an individual element of the array, we use the index notation array(index).

In this program, we assign the value 3 to the variable index and then print the value at that index using print *, “Value at index”, index, “: “, numbers(index).


program array_indexing_example
  implicit none
  
  ! Declare an array of integers
  integer :: numbers(5) = [10, 20, 30, 40, 50]
  integer :: i, index
  
  ! Access and modify array elements
  index = 3
  print *, "Value at index", index, ": ", numbers(index)
  
  numbers(4) = 45
  
  ! Print the updated array
  do i = 1, 5
    print *, numbers(i)
  end do
  
end program array_indexing_example

More Related Stuff