A weighted average multiplies each value by its weight, sums the products, and divides by the total weight.

Program

Play the program to choose the third weight and watch the average change.

third_weight
weighted_average.f90
Replay: real traced execution (multi-file project)
program weighted_average_demo
    implicit none
    real :: values(3), weights(3)
    real :: weighted_sum, weight_total, average
    real :: third_weight

    values = [10.0, 20.0, 30.0]
    third_weight = 3.0
    weights = [1.0, 2.0, third_weight]
    weighted_sum = sum(values * weights)
    weight_total = sum(weights)
    average = weighted_sum / weight_total
    print '(F0.1)', average
end program weighted_average_demo
program weighted_average_demo
    implicit none
    real :: values(3), weights(3)
    real :: weighted_sum, weight_total, average
    real :: third_weight

    values = [10.0, 20.0, 30.0]
    third_weight = 1.0
    weights = [1.0, 2.0, third_weight]
    weighted_sum = sum(values * weights)
    weight_total = sum(weights)
    average = weighted_sum / weight_total
    print '(F0.1)', average
end program weighted_average_demo
program weighted_average_demo
    implicit none
    real :: values(3), weights(3)
    real :: weighted_sum, weight_total, average
    real :: third_weight

    values = [10.0, 20.0, 30.0]
    third_weight = 5.0
    weights = [1.0, 2.0, third_weight]
    weighted_sum = sum(values * weights)
    weight_total = sum(weights)
    average = weighted_sum / weight_total
    print '(F0.1)', average
end program weighted_average_demo
  1. values ← [10.0, 20.0, 30.0]

    7values = [10.0, 20.0, 30.0]8third_weight = 3.0
    values this step[10.0, 20.0, 30.0]values
  2. third_weight ← 3.0

    7values = [10.0, 20.0, 30.0]8third_weight = 3.09weights = [1.0, 2.0, third_weight]
    values this step3.0third_weight
  3. weights ← [1.0, 2.0, 3.0]

    8third_weight = 3.09weights = [1.0, 2.0, third_weight]10weighted_sum = sum(values * weights)
    values this step[1.0, 2.0, 3.0]weights3.0third_weight
  4. weighted_sum ← 140.0

    9weights = [1.0, 2.0, third_weight]10weighted_sum = sum(values * weights)11weight_total = sum(weights)
    values this step140.0weighted_sum[10.0, 20.0, 30.0]values[1.0, 2.0, 3.0]weights
  5. weight_total ← 6.0

    10weighted_sum = sum(values * weights)11weight_total = sum(weights)12average = weighted_sum / weight_total
    values this step6.0weight_total[1.0, 2.0, 3.0]weights
  6. average ← 23.3

    11weight_total = sum(weights)12average = weighted_sum / weight_total13print '(F0.1)', average
    values this step23.3average140.0weighted_sum6.0weight_total
  7. print '(F0.1)', average

    12    average = weighted_sum / weight_total13    print '(F0.1)', average14end program weighted_average_demo
    output23.3
    values this step23.3average
  1. values ← [10.0, 20.0, 30.0]

    7values = [10.0, 20.0, 30.0]8third_weight = 1.0
    values this step[10.0, 20.0, 30.0]values
  2. third_weight ← 1.0

    7values = [10.0, 20.0, 30.0]8third_weight = 1.09weights = [1.0, 2.0, third_weight]
    values this step1.0third_weight
  3. weights ← [1.0, 2.0, 1.0]

    8third_weight = 1.09weights = [1.0, 2.0, third_weight]10weighted_sum = sum(values * weights)
    values this step[1.0, 2.0, 1.0]weights1.0third_weight
  4. weighted_sum ← 80.0

    9weights = [1.0, 2.0, third_weight]10weighted_sum = sum(values * weights)11weight_total = sum(weights)
    values this step80.0weighted_sum[10.0, 20.0, 30.0]values[1.0, 2.0, 1.0]weights
  5. weight_total ← 4.0

    10weighted_sum = sum(values * weights)11weight_total = sum(weights)12average = weighted_sum / weight_total
    values this step4.0weight_total[1.0, 2.0, 1.0]weights
  6. average ← 20.0

    11weight_total = sum(weights)12average = weighted_sum / weight_total13print '(F0.1)', average
    values this step20.0average80.0weighted_sum4.0weight_total
  7. print '(F0.1)', average

    12    average = weighted_sum / weight_total13    print '(F0.1)', average14end program weighted_average_demo
    output20.0
    values this step20.0average
  1. values ← [10.0, 20.0, 30.0]

    7values = [10.0, 20.0, 30.0]8third_weight = 5.0
    values this step[10.0, 20.0, 30.0]values
  2. third_weight ← 5.0

    7values = [10.0, 20.0, 30.0]8third_weight = 5.09weights = [1.0, 2.0, third_weight]
    values this step5.0third_weight
  3. weights ← [1.0, 2.0, 5.0]

    8third_weight = 5.09weights = [1.0, 2.0, third_weight]10weighted_sum = sum(values * weights)
    values this step[1.0, 2.0, 5.0]weights5.0third_weight
  4. weighted_sum ← 200.0

    9weights = [1.0, 2.0, third_weight]10weighted_sum = sum(values * weights)11weight_total = sum(weights)
    values this step200.0weighted_sum[10.0, 20.0, 30.0]values[1.0, 2.0, 5.0]weights
  5. weight_total ← 8.0

    10weighted_sum = sum(values * weights)11weight_total = sum(weights)12average = weighted_sum / weight_total
    values this step8.0weight_total[1.0, 2.0, 5.0]weights
  6. average ← 25.0

    11weight_total = sum(weights)12average = weighted_sum / weight_total13print '(F0.1)', average
    values this step25.0average200.0weighted_sum8.0weight_total
  7. print '(F0.1)', average

    12    average = weighted_sum / weight_total13    print '(F0.1)', average14end program weighted_average_demo
    output25.0
    values this step25.0average
array product `values * weights` multiplies corresponding elements.
sum `sum(...)` reduces an array expression to one scalar.
weighted average Dividing by `sum(weights)` keeps the result on the original value scale.