A mean is a sum divided by the number of active observations.

Program

Play the program to change how many values contribute to the mean.

active_count
mean_with_count.f90
Replay: real traced execution (multi-file project)
program mean_with_count_demo
    implicit none
    integer :: values(4)
    integer :: active_count
    integer :: i
    integer :: total
    real :: mean

    values = [4, 6, 11, 15]
    active_count = 3
    total = 0
    do i = 1, active_count
        total = total + values(i)
    end do
    mean = real(total) / real(active_count)
    print '(I0, 1X, F0.1)', active_count, mean
end program mean_with_count_demo
program mean_with_count_demo
    implicit none
    integer :: values(4)
    integer :: active_count
    integer :: i
    integer :: total
    real :: mean

    values = [4, 6, 11, 15]
    active_count = 2
    total = 0
    do i = 1, active_count
        total = total + values(i)
    end do
    mean = real(total) / real(active_count)
    print '(I0, 1X, F0.1)', active_count, mean
end program mean_with_count_demo
program mean_with_count_demo
    implicit none
    integer :: values(4)
    integer :: active_count
    integer :: i
    integer :: total
    real :: mean

    values = [4, 6, 11, 15]
    active_count = 4
    total = 0
    do i = 1, active_count
        total = total + values(i)
    end do
    mean = real(total) / real(active_count)
    print '(I0, 1X, F0.1)', active_count, mean
end program mean_with_count_demo
  1. values ← [4, 6, 11, 15]

    9values = [4, 6, 11, 15]10active_count = 3
    values this step[4, 6, 11, 15]values
  2. active_count ← 3

    9values = [4, 6, 11, 15]10active_count = 311total = 0
    values this step3active_count
  3. total ← 0

    10active_count = 311total = 012do i = 1, active_count
    values this step0total
  4. i ← 1

    11total = 012do i = 1, active_count13    total = total + values(i)
    values this step1i
  5. total ← 4

    12do i = 1, active_count13    total = total + values(i)14end do
    values this step0 4total4values(1)
  6. i ← 2

    11total = 012do i = 1, active_count13    total = total + values(i)
    values this step2i
  7. total ← 10

    12do i = 1, active_count13    total = total + values(i)14end do
    values this step4 10total6values(2)
  8. i ← 3

    11total = 012do i = 1, active_count13    total = total + values(i)
    values this step3i
  9. total ← 21

    12do i = 1, active_count13    total = total + values(i)14end do
    values this step10 21total11values(3)
  10. mean ← 7.0

    14end do15mean = real(total) / real(active_count)16print '(I0, 1X, F0.1)', active_count, mean
    values this step7.0mean21total3active_count
  11. print '(I0, 1X, F0.1)', active_count, mean

    15    mean = real(total) / real(active_count)16    print '(I0, 1X, F0.1)', active_count, mean17end program mean_with_count_demo
    output3 7.0
    values this step3active_count7.0mean
  1. values ← [4, 6, 11, 15]

    9values = [4, 6, 11, 15]10active_count = 2
    values this step[4, 6, 11, 15]values
  2. active_count ← 2

    9values = [4, 6, 11, 15]10active_count = 211total = 0
    values this step2active_count
  3. total ← 0

    10active_count = 211total = 012do i = 1, active_count
    values this step0total
  4. i ← 1

    11total = 012do i = 1, active_count13    total = total + values(i)
    values this step1i
  5. total ← 4

    12do i = 1, active_count13    total = total + values(i)14end do
    values this step0 4total4values(1)
  6. i ← 2

    11total = 012do i = 1, active_count13    total = total + values(i)
    values this step2i
  7. total ← 10

    12do i = 1, active_count13    total = total + values(i)14end do
    values this step4 10total6values(2)
  8. mean ← 5.0

    14end do15mean = real(total) / real(active_count)16print '(I0, 1X, F0.1)', active_count, mean
    values this step5.0mean10total2active_count
  9. print '(I0, 1X, F0.1)', active_count, mean

    15    mean = real(total) / real(active_count)16    print '(I0, 1X, F0.1)', active_count, mean17end program mean_with_count_demo
    output2 5.0
    values this step2active_count5.0mean
  1. values ← [4, 6, 11, 15]

    9values = [4, 6, 11, 15]10active_count = 4
    values this step[4, 6, 11, 15]values
  2. active_count ← 4

    9values = [4, 6, 11, 15]10active_count = 411total = 0
    values this step4active_count
  3. total ← 0

    10active_count = 411total = 012do i = 1, active_count
    values this step0total
  4. i ← 1

    11total = 012do i = 1, active_count13    total = total + values(i)
    values this step1i
  5. total ← 4

    12do i = 1, active_count13    total = total + values(i)14end do
    values this step0 4total4values(1)
  6. i ← 2

    11total = 012do i = 1, active_count13    total = total + values(i)
    values this step2i
  7. total ← 10

    12do i = 1, active_count13    total = total + values(i)14end do
    values this step4 10total6values(2)
  8. i ← 3

    11total = 012do i = 1, active_count13    total = total + values(i)
    values this step3i
  9. total ← 21

    12do i = 1, active_count13    total = total + values(i)14end do
    values this step10 21total11values(3)
  10. i ← 4

    11total = 012do i = 1, active_count13    total = total + values(i)
    values this step4i
  11. total ← 36

    12do i = 1, active_count13    total = total + values(i)14end do
    values this step21 36total15values(4)
  12. mean ← 9.0

    14end do15mean = real(total) / real(active_count)16print '(I0, 1X, F0.1)', active_count, mean
    values this step9.0mean36total4active_count
  13. print '(I0, 1X, F0.1)', active_count, mean

    15    mean = real(total) / real(active_count)16    print '(I0, 1X, F0.1)', active_count, mean17end program mean_with_count_demo
    output4 9.0
    values this step4active_count9.0mean
active count `active_count` controls how much of the fixed array is summarized.
running total The loop adds each active observation into `total`.
real division `real(total) / real(active_count)` keeps the mean as a real number.