A grouped summary filters rows by group before it counts and sums them.

Program

Play the program to choose which group contributes to the average.

target_group
group_average.f90
Replay: real traced execution (multi-file project)
program group_average_demo
    implicit none
    integer :: groups(5)
    integer :: scores(5)
    integer :: target_group
    integer :: i
    integer :: count
    integer :: total
    integer :: average

    groups = [1, 2, 1, 3, 2]
    scores = [70, 80, 90, 90, 90]
    target_group = 1
    count = 0
    total = 0
    do i = 1, 5
        if (groups(i) == target_group) then
            count = count + 1
            total = total + scores(i)
        end if
    end do
    average = total / count
    print '(I0, 1X, I0, 1X, I0)', target_group, count, average
end program group_average_demo
program group_average_demo
    implicit none
    integer :: groups(5)
    integer :: scores(5)
    integer :: target_group
    integer :: i
    integer :: count
    integer :: total
    integer :: average

    groups = [1, 2, 1, 3, 2]
    scores = [70, 80, 90, 90, 90]
    target_group = 2
    count = 0
    total = 0
    do i = 1, 5
        if (groups(i) == target_group) then
            count = count + 1
            total = total + scores(i)
        end if
    end do
    average = total / count
    print '(I0, 1X, I0, 1X, I0)', target_group, count, average
end program group_average_demo
program group_average_demo
    implicit none
    integer :: groups(5)
    integer :: scores(5)
    integer :: target_group
    integer :: i
    integer :: count
    integer :: total
    integer :: average

    groups = [1, 2, 1, 3, 2]
    scores = [70, 80, 90, 90, 90]
    target_group = 3
    count = 0
    total = 0
    do i = 1, 5
        if (groups(i) == target_group) then
            count = count + 1
            total = total + scores(i)
        end if
    end do
    average = total / count
    print '(I0, 1X, I0, 1X, I0)', target_group, count, average
end program group_average_demo
  1. groups ← [1, 2, 1, 3, 2]

    11groups = [1, 2, 1, 3, 2]12scores = [70, 80, 90, 90, 90]
    values this step[1, 2, 1, 3, 2]groups
  2. scores ← [70, 80, 90, 90, 90]

    11groups = [1, 2, 1, 3, 2]12scores = [70, 80, 90, 90, 90]13target_group = 1
    values this step[70, 80, 90, 90, 90]scores
  3. target_group ← 1

    12scores = [70, 80, 90, 90, 90]13target_group = 114count = 0
    values this step1target_group
  4. count ← 0

    13target_group = 114count = 015total = 0
    values this step0count
  5. total ← 0

    14count = 015total = 016do i = 1, 5
    values this step0total
  6. i ← 1

    15total = 016do i = 1, 517    if (groups(i) == target_group) then
    values this step1i
  7. if (groups(i) == target_group) then

    16do i = 1, 517    if (groups(i) == target_group) then18        count = count + 1
    values this step.true.groups(1) == target_group
  8. count ← 1

    17if (groups(i) == target_group) then18    count = count + 119    total = total + scores(i)
    values this step0 1count
  9. total ← 70

    18    count = count + 119    total = total + scores(i)20end if
    values this step0 70total70scores(1)
  10. i ← 2

    15total = 016do i = 1, 517    if (groups(i) == target_group) then
    values this step2i
  11. if (groups(i) == target_group) then

    16do i = 1, 517    if (groups(i) == target_group) then18        count = count + 1
    values this step.false.groups(2) == target_group
  12. i ← 3

    15total = 016do i = 1, 517    if (groups(i) == target_group) then
    values this step3i
  13. if (groups(i) == target_group) then

    16do i = 1, 517    if (groups(i) == target_group) then18        count = count + 1
    values this step.true.groups(3) == target_group
  14. count ← 2

    17if (groups(i) == target_group) then18    count = count + 119    total = total + scores(i)
    values this step1 2count
  15. total ← 160

    18    count = count + 119    total = total + scores(i)20end if
    values this step70 160total90scores(3)
  16. i ← 4

    15total = 016do i = 1, 517    if (groups(i) == target_group) then
    values this step4i
  17. if (groups(i) == target_group) then

    16do i = 1, 517    if (groups(i) == target_group) then18        count = count + 1
    values this step.false.groups(4) == target_group
  18. i ← 5

    15total = 016do i = 1, 517    if (groups(i) == target_group) then
    values this step5i
  19. if (groups(i) == target_group) then

    16do i = 1, 517    if (groups(i) == target_group) then18        count = count + 1
    values this step.false.groups(5) == target_group
  20. average ← 80

    21end do22average = total / count23print '(I0, 1X, I0, 1X, I0)', target_group, count, average
    values this step80average160total2count
  21. print '(I0, 1X, I0, 1X, I0)', target_group, count, average

    22    average = total / count23    print '(I0, 1X, I0, 1X, I0)', target_group, count, average24end program group_average_demo
    output1 2 80
    values this step1target_group2count80average
  1. groups ← [1, 2, 1, 3, 2]

    11groups = [1, 2, 1, 3, 2]12scores = [70, 80, 90, 90, 90]
    values this step[1, 2, 1, 3, 2]groups
  2. scores ← [70, 80, 90, 90, 90]

    11groups = [1, 2, 1, 3, 2]12scores = [70, 80, 90, 90, 90]13target_group = 2
    values this step[70, 80, 90, 90, 90]scores
  3. target_group ← 2

    12scores = [70, 80, 90, 90, 90]13target_group = 214count = 0
    values this step2target_group
  4. count ← 0

    13target_group = 214count = 015total = 0
    values this step0count
  5. total ← 0

    14count = 015total = 016do i = 1, 5
    values this step0total
  6. i ← 1

    15total = 016do i = 1, 517    if (groups(i) == target_group) then
    values this step1i
  7. if (groups(i) == target_group) then

    16do i = 1, 517    if (groups(i) == target_group) then18        count = count + 1
    values this step.false.groups(1) == target_group
  8. i ← 2

    15total = 016do i = 1, 517    if (groups(i) == target_group) then
    values this step2i
  9. if (groups(i) == target_group) then

    16do i = 1, 517    if (groups(i) == target_group) then18        count = count + 1
    values this step.true.groups(2) == target_group
  10. count ← 1

    17if (groups(i) == target_group) then18    count = count + 119    total = total + scores(i)
    values this step0 1count
  11. total ← 80

    18    count = count + 119    total = total + scores(i)20end if
    values this step0 80total80scores(2)
  12. i ← 3

    15total = 016do i = 1, 517    if (groups(i) == target_group) then
    values this step3i
  13. if (groups(i) == target_group) then

    16do i = 1, 517    if (groups(i) == target_group) then18        count = count + 1
    values this step.false.groups(3) == target_group
  14. i ← 4

    15total = 016do i = 1, 517    if (groups(i) == target_group) then
    values this step4i
  15. if (groups(i) == target_group) then

    16do i = 1, 517    if (groups(i) == target_group) then18        count = count + 1
    values this step.false.groups(4) == target_group
  16. i ← 5

    15total = 016do i = 1, 517    if (groups(i) == target_group) then
    values this step5i
  17. if (groups(i) == target_group) then

    16do i = 1, 517    if (groups(i) == target_group) then18        count = count + 1
    values this step.true.groups(5) == target_group
  18. count ← 2

    17if (groups(i) == target_group) then18    count = count + 119    total = total + scores(i)
    values this step1 2count
  19. total ← 170

    18    count = count + 119    total = total + scores(i)20end if
    values this step80 170total90scores(5)
  20. average ← 85

    21end do22average = total / count23print '(I0, 1X, I0, 1X, I0)', target_group, count, average
    values this step85average170total2count
  21. print '(I0, 1X, I0, 1X, I0)', target_group, count, average

    22    average = total / count23    print '(I0, 1X, I0, 1X, I0)', target_group, count, average24end program group_average_demo
    output2 2 85
    values this step2target_group2count85average
  1. groups ← [1, 2, 1, 3, 2]

    11groups = [1, 2, 1, 3, 2]12scores = [70, 80, 90, 90, 90]
    values this step[1, 2, 1, 3, 2]groups
  2. scores ← [70, 80, 90, 90, 90]

    11groups = [1, 2, 1, 3, 2]12scores = [70, 80, 90, 90, 90]13target_group = 3
    values this step[70, 80, 90, 90, 90]scores
  3. target_group ← 3

    12scores = [70, 80, 90, 90, 90]13target_group = 314count = 0
    values this step3target_group
  4. count ← 0

    13target_group = 314count = 015total = 0
    values this step0count
  5. total ← 0

    14count = 015total = 016do i = 1, 5
    values this step0total
  6. i ← 1

    15total = 016do i = 1, 517    if (groups(i) == target_group) then
    values this step1i
  7. if (groups(i) == target_group) then

    16do i = 1, 517    if (groups(i) == target_group) then18        count = count + 1
    values this step.false.groups(1) == target_group
  8. i ← 2

    15total = 016do i = 1, 517    if (groups(i) == target_group) then
    values this step2i
  9. if (groups(i) == target_group) then

    16do i = 1, 517    if (groups(i) == target_group) then18        count = count + 1
    values this step.false.groups(2) == target_group
  10. i ← 3

    15total = 016do i = 1, 517    if (groups(i) == target_group) then
    values this step3i
  11. if (groups(i) == target_group) then

    16do i = 1, 517    if (groups(i) == target_group) then18        count = count + 1
    values this step.false.groups(3) == target_group
  12. i ← 4

    15total = 016do i = 1, 517    if (groups(i) == target_group) then
    values this step4i
  13. if (groups(i) == target_group) then

    16do i = 1, 517    if (groups(i) == target_group) then18        count = count + 1
    values this step.true.groups(4) == target_group
  14. count ← 1

    17if (groups(i) == target_group) then18    count = count + 119    total = total + scores(i)
    values this step0 1count
  15. total ← 90

    18    count = count + 119    total = total + scores(i)20end if
    values this step0 90total90scores(4)
  16. i ← 5

    15total = 016do i = 1, 517    if (groups(i) == target_group) then
    values this step5i
  17. if (groups(i) == target_group) then

    16do i = 1, 517    if (groups(i) == target_group) then18        count = count + 1
    values this step.false.groups(5) == target_group
  18. average ← 90

    21end do22average = total / count23print '(I0, 1X, I0, 1X, I0)', target_group, count, average
    values this step90average90total1count
  19. print '(I0, 1X, I0, 1X, I0)', target_group, count, average

    22    average = total / count23    print '(I0, 1X, I0, 1X, I0)', target_group, count, average24end program group_average_demo
    output3 1 90
    values this step3target_group1count90average
group filter Only rows whose group matches `target_group` enter the summary.
count and total The same branch increments the row count and adds the score.
integer average This example keeps the average as an integer for compact output.