A small table can hold rates while an index chooses which row is active.

Program

Play the program to choose a different rate row and recompute the charge.

row
rate_lookup_table.f90
Replay: real traced execution (multi-file project)
program rate_lookup_table_demo
    implicit none
    integer :: rate_cents(3)
    integer :: row
    integer :: units
    integer :: charge_cents

    rate_cents = [12, 15, 20]
    row = 2
    units = 8
    charge_cents = units * rate_cents(row)
    print '(I0, 1X, I0)', row, charge_cents
end program rate_lookup_table_demo
program rate_lookup_table_demo
    implicit none
    integer :: rate_cents(3)
    integer :: row
    integer :: units
    integer :: charge_cents

    rate_cents = [12, 15, 20]
    row = 1
    units = 8
    charge_cents = units * rate_cents(row)
    print '(I0, 1X, I0)', row, charge_cents
end program rate_lookup_table_demo
program rate_lookup_table_demo
    implicit none
    integer :: rate_cents(3)
    integer :: row
    integer :: units
    integer :: charge_cents

    rate_cents = [12, 15, 20]
    row = 3
    units = 8
    charge_cents = units * rate_cents(row)
    print '(I0, 1X, I0)', row, charge_cents
end program rate_lookup_table_demo
  1. rate_cents ← [12, 15, 20]

    8rate_cents = [12, 15, 20]9row = 2
    values this step[12, 15, 20]rate_cents
  2. row ← 2

    8rate_cents = [12, 15, 20]9row = 210units = 8
    values this step2row
  3. units ← 8

    9row = 210units = 811charge_cents = units * rate_cents(row)
    values this step8units
  4. charge_cents ← 120

    10units = 811charge_cents = units * rate_cents(row)12print '(I0, 1X, I0)', row, charge_cents
    values this step120charge_cents8units15rate_cents(2)
  5. print '(I0, 1X, I0)', row, charge_cents

    11    charge_cents = units * rate_cents(row)12    print '(I0, 1X, I0)', row, charge_cents13end program rate_lookup_table_demo
    output2 120
    values this step2row120charge_cents
  1. rate_cents ← [12, 15, 20]

    8rate_cents = [12, 15, 20]9row = 1
    values this step[12, 15, 20]rate_cents
  2. row ← 1

    8rate_cents = [12, 15, 20]9row = 110units = 8
    values this step1row
  3. units ← 8

    9row = 110units = 811charge_cents = units * rate_cents(row)
    values this step8units
  4. charge_cents ← 96

    10units = 811charge_cents = units * rate_cents(row)12print '(I0, 1X, I0)', row, charge_cents
    values this step96charge_cents8units12rate_cents(1)
  5. print '(I0, 1X, I0)', row, charge_cents

    11    charge_cents = units * rate_cents(row)12    print '(I0, 1X, I0)', row, charge_cents13end program rate_lookup_table_demo
    output1 96
    values this step1row96charge_cents
  1. rate_cents ← [12, 15, 20]

    8rate_cents = [12, 15, 20]9row = 3
    values this step[12, 15, 20]rate_cents
  2. row ← 3

    8rate_cents = [12, 15, 20]9row = 310units = 8
    values this step3row
  3. units ← 8

    9row = 310units = 811charge_cents = units * rate_cents(row)
    values this step8units
  4. charge_cents ← 160

    10units = 811charge_cents = units * rate_cents(row)12print '(I0, 1X, I0)', row, charge_cents
    values this step160charge_cents8units20rate_cents(3)
  5. print '(I0, 1X, I0)', row, charge_cents

    11    charge_cents = units * rate_cents(row)12    print '(I0, 1X, I0)', row, charge_cents13end program rate_lookup_table_demo
    output3 160
    values this step3row160charge_cents
lookup table A table turns row data into values that code can reuse.
row index `rate_cents(row)` selects one rate from the table.
derived value `charge_cents` is calculated from the selected table row.