A calculation table can pair values with weights and accumulate row contributions.

Program

Play the program to include more rows in the weighted total.

row_count
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
  1. values ← [5, 7, 9]

    10values = [5, 7, 9]11weights = [2, 3, 4]
    values this step[5, 7, 9]values
  2. weights ← [2, 3, 4]

    10values = [5, 7, 9]11weights = [2, 3, 4]12row_count = 2
    values this step[2, 3, 4]weights
  3. row_count ← 2

    11weights = [2, 3, 4]12row_count = 213total = 0
    values this step2row_count
  4. total ← 0

    12row_count = 213total = 014do i = 1, row_count
    values this step0total
  5. i ← 1

    13total = 014do i = 1, row_count15    contribution = values(i) * weights(i)
    values this step1i
  6. contribution ← 10

    14do i = 1, row_count15    contribution = values(i) * weights(i)16    total = total + contribution
    values this step10contribution5values(1)2weights(1)
  7. total ← 10

    15    contribution = values(i) * weights(i)16    total = total + contribution17end do
    values this step0 10total10contribution
  8. i ← 2

    13total = 014do i = 1, row_count15    contribution = values(i) * weights(i)
    values this step2i
  9. contribution ← 21

    14do i = 1, row_count15    contribution = values(i) * weights(i)16    total = total + contribution
    values this step21contribution7values(2)3weights(2)
  10. total ← 31

    15    contribution = values(i) * weights(i)16    total = total + contribution17end do
    values this step10 31total21contribution
  11. print '(I0, 1X, I0)', row_count, total

    17    end do18    print '(I0, 1X, I0)', row_count, total19end program weighted_table_total_demo
    output2 31
    values this step2row_count31total
  1. values ← [5, 7, 9]

    10values = [5, 7, 9]11weights = [2, 3, 4]
    values this step[5, 7, 9]values
  2. weights ← [2, 3, 4]

    10values = [5, 7, 9]11weights = [2, 3, 4]12row_count = 1
    values this step[2, 3, 4]weights
  3. row_count ← 1

    11weights = [2, 3, 4]12row_count = 113total = 0
    values this step1row_count
  4. total ← 0

    12row_count = 113total = 014do i = 1, row_count
    values this step0total
  5. i ← 1

    13total = 014do i = 1, row_count15    contribution = values(i) * weights(i)
    values this step1i
  6. contribution ← 10

    14do i = 1, row_count15    contribution = values(i) * weights(i)16    total = total + contribution
    values this step10contribution5values(1)2weights(1)
  7. total ← 10

    15    contribution = values(i) * weights(i)16    total = total + contribution17end do
    values this step0 10total10contribution
  8. print '(I0, 1X, I0)', row_count, total

    17    end do18    print '(I0, 1X, I0)', row_count, total19end program weighted_table_total_demo
    output1 10
    values this step1row_count10total
  1. values ← [5, 7, 9]

    10values = [5, 7, 9]11weights = [2, 3, 4]
    values this step[5, 7, 9]values
  2. weights ← [2, 3, 4]

    10values = [5, 7, 9]11weights = [2, 3, 4]12row_count = 3
    values this step[2, 3, 4]weights
  3. row_count ← 3

    11weights = [2, 3, 4]12row_count = 313total = 0
    values this step3row_count
  4. total ← 0

    12row_count = 313total = 014do i = 1, row_count
    values this step0total
  5. i ← 1

    13total = 014do i = 1, row_count15    contribution = values(i) * weights(i)
    values this step1i
  6. contribution ← 10

    14do i = 1, row_count15    contribution = values(i) * weights(i)16    total = total + contribution
    values this step10contribution5values(1)2weights(1)
  7. total ← 10

    15    contribution = values(i) * weights(i)16    total = total + contribution17end do
    values this step0 10total10contribution
  8. i ← 2

    13total = 014do i = 1, row_count15    contribution = values(i) * weights(i)
    values this step2i
  9. contribution ← 21

    14do i = 1, row_count15    contribution = values(i) * weights(i)16    total = total + contribution
    values this step21contribution7values(2)3weights(2)
  10. total ← 31

    15    contribution = values(i) * weights(i)16    total = total + contribution17end do
    values this step10 31total21contribution
  11. i ← 3

    13total = 014do i = 1, row_count15    contribution = values(i) * weights(i)
    values this step3i
  12. contribution ← 36

    14do i = 1, row_count15    contribution = values(i) * weights(i)16    total = total + contribution
    values this step36contribution9values(3)4weights(3)
  13. total ← 67

    15    contribution = values(i) * weights(i)16    total = total + contribution17end do
    values this step31 67total36contribution
  14. print '(I0, 1X, I0)', row_count, total

    17    end do18    print '(I0, 1X, I0)', row_count, total19end program weighted_table_total_demo
    output3 67
    values 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.