Skip to main content
Engineering LibreTexts

13.3: Accessing Array Elements

  • Page ID
    54346
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    To access elements in an array, the array name and an index must be specified. The index must be an integer or integer expression and enclosed in parentheses. The general format is,

    array-name(<integer expression>)
    

    For example, given the declaration,

    real, dimension(10) :: times
    

    would declare an array with ten elements. To place a value 121.3 in the first array element,

    times(1) = 121.3
    

    And to place 98.125 in the fifth element,

    times(5) = 98.125
    

    The index in these examples is a literal. However, the index can be an integer variable or integer expression.

    For example, given the following declarations,

    real, dimension(10) :: temps
    integer :: i=5, j
    

    would declare an array with ten elements. To place a value 98.6 in the fifth array element,

    temps(i) = 98.6
    

    To access the fifth element, subtract 3.0 and place the result in the sixth element,

    temps(i+1) = temps(i) – 3.0
    

    To set all elements of the temps array to 0.0, a loop could be used as follows:

    do i = 1, 10
        temps(i) = 0.0
    end do
    

    Array elements can be accessed as many times as needed.

    Array Bounds

    When an array is declared for a specific size, only that many elements can be used. For example, if an array is declared with 10 elements, only 10 elements are available. Given the following declaration,

    real, dimension(10) :: expArr
    

    would declare an array with ten elements.

    To place a value 42.5 in the first array element and 73.5 in the last array element.

    expArr(1) = 42.5
    expArr(10) = 73.5
    

    However, if an array element is accessed that is outside the declared bounds, it is an error. For example,

    expArr(11) = 99.5
    

    would be an error. These kinds of errors would be difficult to find. However, if the bounds checking is turned on (as noted in chapter 3), when the program is executed the error will be noted.

    To compile with bounds checking turned on, the following compile command should be used:

    C:\fortran> gfortran -fcheck=bounds -o hw hw.f95
    

    This command will tell the 'gfortran' compiler to include bounds checking.

    In general, using the bounds checking can slow a program down. However, this is not a significant issue when learning to write programs.


    This page titled 13.3: Accessing Array Elements is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?