Table-Driven Calculations
Weighted Table Total
Row Accumulation
A calculation table can pair values with weights and accumulate row contributions.
Program
Play the program to include more rows in the weighted total.
weighted_table_total.f90
Replay: real traced execution (multi-file project)
program weighted_table_total_demo
implicit none
integer :: values(3)
integer :: weights(3)
integer :: row_count
integer :: i
integer :: total
integer :: contribution
values = [5, 7, 9]
weights = [2, 3, 4]
row_count = 2
total = 0
do i = 1, row_count
contribution = values(i) * weights(i)
total = total + contribution
end do
print '(I0, 1X, I0)', row_count, total
end program weighted_table_total_demo
program weighted_table_total_demo
implicit none
integer :: values(3)
integer :: weights(3)
integer :: row_count
integer :: i
integer :: total
integer :: contribution
values = [5, 7, 9]
weights = [2, 3, 4]
row_count = 1
total = 0
do i = 1, row_count
contribution = values(i) * weights(i)
total = total + contribution
end do
print '(I0, 1X, I0)', row_count, total
end program weighted_table_total_demo
program weighted_table_total_demo
implicit none
integer :: values(3)
integer :: weights(3)
integer :: row_count
integer :: i
integer :: total
integer :: contribution
values = [5, 7, 9]
weights = [2, 3, 4]
row_count = 3
total = 0
do i = 1, row_count
contribution = values(i) * weights(i)
total = total + contribution
end do
print '(I0, 1X, I0)', row_count, total
end program weighted_table_total_demo
values ← [5, 7, 9]
10values = [5, 7, 9]11weights = [2, 3, 4]values this step[5, 7, 9]valuesweights ← [2, 3, 4]
10values = [5, 7, 9]11weights = [2, 3, 4]12row_count = 2values this step[2, 3, 4]weightsrow_count ← 2
11weights = [2, 3, 4]12row_count = 213total = 0values this step2row_counttotal ← 0
12row_count = 213total = 014do i = 1, row_countvalues this step0totali ← 1
13total = 014do i = 1, row_count15 contribution = values(i) * weights(i)values this step1icontribution ← 10
14do i = 1, row_count15 contribution = values(i) * weights(i)16 total = total + contributionvalues this step10contribution5values(1)2weights(1)total ← 10
15 contribution = values(i) * weights(i)16 total = total + contribution17end dovalues this step0 → 10total10contributioni ← 2
13total = 014do i = 1, row_count15 contribution = values(i) * weights(i)values this step2icontribution ← 21
14do i = 1, row_count15 contribution = values(i) * weights(i)16 total = total + contributionvalues this step21contribution7values(2)3weights(2)total ← 31
15 contribution = values(i) * weights(i)16 total = total + contribution17end dovalues this step10 → 31total21contributionprint '(I0, 1X, I0)', row_count, total
17 end do18 print '(I0, 1X, I0)', row_count, total19end program weighted_table_total_demooutput2 31values this step2row_count31total
values ← [5, 7, 9]
10values = [5, 7, 9]11weights = [2, 3, 4]values this step[5, 7, 9]valuesweights ← [2, 3, 4]
10values = [5, 7, 9]11weights = [2, 3, 4]12row_count = 1values this step[2, 3, 4]weightsrow_count ← 1
11weights = [2, 3, 4]12row_count = 113total = 0values this step1row_counttotal ← 0
12row_count = 113total = 014do i = 1, row_countvalues this step0totali ← 1
13total = 014do i = 1, row_count15 contribution = values(i) * weights(i)values this step1icontribution ← 10
14do i = 1, row_count15 contribution = values(i) * weights(i)16 total = total + contributionvalues this step10contribution5values(1)2weights(1)total ← 10
15 contribution = values(i) * weights(i)16 total = total + contribution17end dovalues this step0 → 10total10contributionprint '(I0, 1X, I0)', row_count, total
17 end do18 print '(I0, 1X, I0)', row_count, total19end program weighted_table_total_demooutput1 10values this step1row_count10total
values ← [5, 7, 9]
10values = [5, 7, 9]11weights = [2, 3, 4]values this step[5, 7, 9]valuesweights ← [2, 3, 4]
10values = [5, 7, 9]11weights = [2, 3, 4]12row_count = 3values this step[2, 3, 4]weightsrow_count ← 3
11weights = [2, 3, 4]12row_count = 313total = 0values this step3row_counttotal ← 0
12row_count = 313total = 014do i = 1, row_countvalues this step0totali ← 1
13total = 014do i = 1, row_count15 contribution = values(i) * weights(i)values this step1icontribution ← 10
14do i = 1, row_count15 contribution = values(i) * weights(i)16 total = total + contributionvalues this step10contribution5values(1)2weights(1)total ← 10
15 contribution = values(i) * weights(i)16 total = total + contribution17end dovalues this step0 → 10total10contributioni ← 2
13total = 014do i = 1, row_count15 contribution = values(i) * weights(i)values this step2icontribution ← 21
14do i = 1, row_count15 contribution = values(i) * weights(i)16 total = total + contributionvalues this step21contribution7values(2)3weights(2)total ← 31
15 contribution = values(i) * weights(i)16 total = total + contribution17end dovalues this step10 → 31total21contributioni ← 3
13total = 014do i = 1, row_count15 contribution = values(i) * weights(i)values this step3icontribution ← 36
14do i = 1, row_count15 contribution = values(i) * weights(i)16 total = total + contributionvalues this step36contribution9values(3)4weights(3)total ← 67
15 contribution = values(i) * weights(i)16 total = total + contribution17end dovalues this step31 → 67total36contributionprint '(I0, 1X, I0)', row_count, total
17 end do18 print '(I0, 1X, I0)', row_count, total19end program weighted_table_total_demooutput3 67values this step3row_count67total
paired columns
`values(i)` and `weights(i)` are matching columns in the same row.
contribution
Each row contribution is calculated before it is added to the total.
partial table
`row_count` lets a calculation use only the active rows.