Table-Driven Calculations
Rate Lookup Table
Indexed Choice
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.
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
rate_cents ← [12, 15, 20]
8rate_cents = [12, 15, 20]9row = 2values this step[12, 15, 20]rate_centsrow ← 2
8rate_cents = [12, 15, 20]9row = 210units = 8values this step2rowunits ← 8
9row = 210units = 811charge_cents = units * rate_cents(row)values this step8unitscharge_cents ← 120
10units = 811charge_cents = units * rate_cents(row)12print '(I0, 1X, I0)', row, charge_centsvalues this step120charge_cents8units15rate_cents(2)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_demooutput2 120values this step2row120charge_cents
rate_cents ← [12, 15, 20]
8rate_cents = [12, 15, 20]9row = 1values this step[12, 15, 20]rate_centsrow ← 1
8rate_cents = [12, 15, 20]9row = 110units = 8values this step1rowunits ← 8
9row = 110units = 811charge_cents = units * rate_cents(row)values this step8unitscharge_cents ← 96
10units = 811charge_cents = units * rate_cents(row)12print '(I0, 1X, I0)', row, charge_centsvalues this step96charge_cents8units12rate_cents(1)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_demooutput1 96values this step1row96charge_cents
rate_cents ← [12, 15, 20]
8rate_cents = [12, 15, 20]9row = 3values this step[12, 15, 20]rate_centsrow ← 3
8rate_cents = [12, 15, 20]9row = 310units = 8values this step3rowunits ← 8
9row = 310units = 811charge_cents = units * rate_cents(row)values this step8unitscharge_cents ← 160
10units = 811charge_cents = units * rate_cents(row)12print '(I0, 1X, I0)', row, charge_centsvalues this step160charge_cents8units20rate_cents(3)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_demooutput3 160values 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.