Skip to main content
Engineering LibreTexts

14.3: Accessing Array Elements

  • Page ID
    54354
  • \( \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 the an index must be specified. The index must include an integer or integer expression for each dimension enclosed in parentheses. The general format is,

    array-name(<integer expression>, <integer expression>)
    

    For example, given the declaration,

    real, dimension(10,5) :: table1
    

    would declare an array, table1, with a total of 50 elements. To place a value 121.3 in the first row and first column,

    table1(1,1) = 121.3
    

    And to place 98.125 in the tenth row and fifth column,

    table1(10,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,10) :: tmptable
    integer :: i=2, j=3
    

    would declare an array, tmptable, with one hundred elements. To place a value 98.6 in the second row, fourth column,

    tmptable(i,j+1) = 98.6
    

    To access the same element, subtract 3.0 and place the result back into the same location,

    tmptable(i,j+1) = tmptable(i,j+1) – 3.0
    

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

    do i = 1, 10
        do j = 1, 10
            tmptable(i,j) = 0.0
        end do
    end do
    

    In addition, the entire array can be set to 0 in the following statement,

    tmptable = 0.0
    

    Array elements can be accessed as many times as needed.


    This page titled 14.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?