Nested loops evaluate every pair from two parameter lists and keep the best combination.

Program

Play the program to include more width choices in the sweep.

width_count
nested_sweep_area.f90
Replay: real traced execution (multi-file project)
program nested_sweep_area_demo
    implicit none
    integer :: widths(3)
    integer :: heights(2)
    integer :: width_count
    integer :: height_count
    integer :: wi, hi
    integer :: width, height, area
    integer :: best_width, best_height, best_area

    widths = [2, 4, 6]
    heights = [3, 5]
    width_count = 2
    height_count = 2
    best_area = -1
    best_width = 0
    best_height = 0
    do wi = 1, width_count
        width = widths(wi)
        do hi = 1, height_count
            height = heights(hi)
            area = width * height
            if (area > best_area) then
                best_area = area
                best_width = width
                best_height = height
            end if
        end do
    end do
    print '(I0, 1X, I0, 1X, I0)', best_width, best_height, best_area
end program nested_sweep_area_demo
program nested_sweep_area_demo
    implicit none
    integer :: widths(3)
    integer :: heights(2)
    integer :: width_count
    integer :: height_count
    integer :: wi, hi
    integer :: width, height, area
    integer :: best_width, best_height, best_area

    widths = [2, 4, 6]
    heights = [3, 5]
    width_count = 1
    height_count = 2
    best_area = -1
    best_width = 0
    best_height = 0
    do wi = 1, width_count
        width = widths(wi)
        do hi = 1, height_count
            height = heights(hi)
            area = width * height
            if (area > best_area) then
                best_area = area
                best_width = width
                best_height = height
            end if
        end do
    end do
    print '(I0, 1X, I0, 1X, I0)', best_width, best_height, best_area
end program nested_sweep_area_demo
program nested_sweep_area_demo
    implicit none
    integer :: widths(3)
    integer :: heights(2)
    integer :: width_count
    integer :: height_count
    integer :: wi, hi
    integer :: width, height, area
    integer :: best_width, best_height, best_area

    widths = [2, 4, 6]
    heights = [3, 5]
    width_count = 3
    height_count = 2
    best_area = -1
    best_width = 0
    best_height = 0
    do wi = 1, width_count
        width = widths(wi)
        do hi = 1, height_count
            height = heights(hi)
            area = width * height
            if (area > best_area) then
                best_area = area
                best_width = width
                best_height = height
            end if
        end do
    end do
    print '(I0, 1X, I0, 1X, I0)', best_width, best_height, best_area
end program nested_sweep_area_demo
  1. widths ← [2, 4, 6]

    11widths = [2, 4, 6]12heights = [3, 5]
    values this step[2, 4, 6]widths
  2. heights ← [3, 5]

    11widths = [2, 4, 6]12heights = [3, 5]13width_count = 2
    values this step[3, 5]heights
  3. width_count ← 2

    12heights = [3, 5]13width_count = 214height_count = 2
    values this step2width_count
  4. height_count ← 2

    13width_count = 214height_count = 215best_area = -1
    values this step2height_count
  5. best_area ← -1

    14height_count = 215best_area = -116best_width = 0
    values this step-1best_area
  6. best_width ← 0

    15best_area = -116best_width = 017best_height = 0
    values this step0best_width
  7. best_height ← 0

    16best_width = 017best_height = 018do wi = 1, width_count
    values this step0best_height
  8. wi ← 1

    17best_height = 018do wi = 1, width_count19    width = widths(wi)
    values this step1wi
  9. width ← 2

    18do wi = 1, width_count19    width = widths(wi)20    do hi = 1, height_count
    values this step2width
  10. hi ← 1

    19width = widths(wi)20do hi = 1, height_count21    height = heights(hi)
    values this step1hi
  11. height ← 3

    20do hi = 1, height_count21    height = heights(hi)22    area = width * height
    values this step3height
  12. area ← 6

    21height = heights(hi)22area = width * height23if (area > best_area) then
    values this step6area2width3height
  13. if (area > best_area) then

    22area = width * height23if (area > best_area) then24    best_area = area
    values this step.true.area > best_area
  14. best_area ← 6

    23if (area > best_area) then24    best_area = area25    best_width = width
    values this step6best_area
  15. best_width ← 2

    24best_area = area25best_width = width26best_height = height
    values this step2best_width
  16. best_height ← 3

    25    best_width = width26    best_height = height27end if
    values this step3best_height
  17. hi ← 2

    19width = widths(wi)20do hi = 1, height_count21    height = heights(hi)
    values this step2hi
  18. height ← 5

    20do hi = 1, height_count21    height = heights(hi)22    area = width * height
    values this step5height
  19. area ← 10

    21height = heights(hi)22area = width * height23if (area > best_area) then
    values this step10area2width5height
  20. if (area > best_area) then

    22area = width * height23if (area > best_area) then24    best_area = area
    values this step.true.area > best_area
  21. best_area ← 10

    23if (area > best_area) then24    best_area = area25    best_width = width
    values this step10best_area
  22. best_width ← 2

    24best_area = area25best_width = width26best_height = height
    values this step2best_width
  23. best_height ← 5

    25    best_width = width26    best_height = height27end if
    values this step5best_height
  24. wi ← 2

    17best_height = 018do wi = 1, width_count19    width = widths(wi)
    values this step2wi
  25. width ← 4

    18do wi = 1, width_count19    width = widths(wi)20    do hi = 1, height_count
    values this step4width
  26. hi ← 1

    19width = widths(wi)20do hi = 1, height_count21    height = heights(hi)
    values this step1hi
  27. height ← 3

    20do hi = 1, height_count21    height = heights(hi)22    area = width * height
    values this step3height
  28. area ← 12

    21height = heights(hi)22area = width * height23if (area > best_area) then
    values this step12area4width3height
  29. if (area > best_area) then

    22area = width * height23if (area > best_area) then24    best_area = area
    values this step.true.area > best_area
  30. best_area ← 12

    23if (area > best_area) then24    best_area = area25    best_width = width
    values this step12best_area
  31. best_width ← 4

    24best_area = area25best_width = width26best_height = height
    values this step4best_width
  32. best_height ← 3

    25    best_width = width26    best_height = height27end if
    values this step3best_height
  33. hi ← 2

    19width = widths(wi)20do hi = 1, height_count21    height = heights(hi)
    values this step2hi
  34. height ← 5

    20do hi = 1, height_count21    height = heights(hi)22    area = width * height
    values this step5height
  35. area ← 20

    21height = heights(hi)22area = width * height23if (area > best_area) then
    values this step20area4width5height
  36. if (area > best_area) then

    22area = width * height23if (area > best_area) then24    best_area = area
    values this step.true.area > best_area
  37. best_area ← 20

    23if (area > best_area) then24    best_area = area25    best_width = width
    values this step20best_area
  38. best_width ← 4

    24best_area = area25best_width = width26best_height = height
    values this step4best_width
  39. best_height ← 5

    25    best_width = width26    best_height = height27end if
    values this step5best_height
  40. print '(I0, 1X, I0, 1X, I0)', best_width, best_height, best_area

    29    end do30    print '(I0, 1X, I0, 1X, I0)', best_width, best_height, best_area31end program nested_sweep_area_demo
    output4 5 20
    values this step4best_width5best_height20best_area
  1. widths ← [2, 4, 6]

    11widths = [2, 4, 6]12heights = [3, 5]
    values this step[2, 4, 6]widths
  2. heights ← [3, 5]

    11widths = [2, 4, 6]12heights = [3, 5]13width_count = 1
    values this step[3, 5]heights
  3. width_count ← 1

    12heights = [3, 5]13width_count = 114height_count = 2
    values this step1width_count
  4. height_count ← 2

    13width_count = 114height_count = 215best_area = -1
    values this step2height_count
  5. best_area ← -1

    14height_count = 215best_area = -116best_width = 0
    values this step-1best_area
  6. best_width ← 0

    15best_area = -116best_width = 017best_height = 0
    values this step0best_width
  7. best_height ← 0

    16best_width = 017best_height = 018do wi = 1, width_count
    values this step0best_height
  8. wi ← 1

    17best_height = 018do wi = 1, width_count19    width = widths(wi)
    values this step1wi
  9. width ← 2

    18do wi = 1, width_count19    width = widths(wi)20    do hi = 1, height_count
    values this step2width
  10. hi ← 1

    19width = widths(wi)20do hi = 1, height_count21    height = heights(hi)
    values this step1hi
  11. height ← 3

    20do hi = 1, height_count21    height = heights(hi)22    area = width * height
    values this step3height
  12. area ← 6

    21height = heights(hi)22area = width * height23if (area > best_area) then
    values this step6area2width3height
  13. if (area > best_area) then

    22area = width * height23if (area > best_area) then24    best_area = area
    values this step.true.area > best_area
  14. best_area ← 6

    23if (area > best_area) then24    best_area = area25    best_width = width
    values this step6best_area
  15. best_width ← 2

    24best_area = area25best_width = width26best_height = height
    values this step2best_width
  16. best_height ← 3

    25    best_width = width26    best_height = height27end if
    values this step3best_height
  17. hi ← 2

    19width = widths(wi)20do hi = 1, height_count21    height = heights(hi)
    values this step2hi
  18. height ← 5

    20do hi = 1, height_count21    height = heights(hi)22    area = width * height
    values this step5height
  19. area ← 10

    21height = heights(hi)22area = width * height23if (area > best_area) then
    values this step10area2width5height
  20. if (area > best_area) then

    22area = width * height23if (area > best_area) then24    best_area = area
    values this step.true.area > best_area
  21. best_area ← 10

    23if (area > best_area) then24    best_area = area25    best_width = width
    values this step10best_area
  22. best_width ← 2

    24best_area = area25best_width = width26best_height = height
    values this step2best_width
  23. best_height ← 5

    25    best_width = width26    best_height = height27end if
    values this step5best_height
  24. print '(I0, 1X, I0, 1X, I0)', best_width, best_height, best_area

    29    end do30    print '(I0, 1X, I0, 1X, I0)', best_width, best_height, best_area31end program nested_sweep_area_demo
    output2 5 10
    values this step2best_width5best_height10best_area
  1. widths ← [2, 4, 6]

    11widths = [2, 4, 6]12heights = [3, 5]
    values this step[2, 4, 6]widths
  2. heights ← [3, 5]

    11widths = [2, 4, 6]12heights = [3, 5]13width_count = 3
    values this step[3, 5]heights
  3. width_count ← 3

    12heights = [3, 5]13width_count = 314height_count = 2
    values this step3width_count
  4. height_count ← 2

    13width_count = 314height_count = 215best_area = -1
    values this step2height_count
  5. best_area ← -1

    14height_count = 215best_area = -116best_width = 0
    values this step-1best_area
  6. best_width ← 0

    15best_area = -116best_width = 017best_height = 0
    values this step0best_width
  7. best_height ← 0

    16best_width = 017best_height = 018do wi = 1, width_count
    values this step0best_height
  8. wi ← 1

    17best_height = 018do wi = 1, width_count19    width = widths(wi)
    values this step1wi
  9. width ← 2

    18do wi = 1, width_count19    width = widths(wi)20    do hi = 1, height_count
    values this step2width
  10. hi ← 1

    19width = widths(wi)20do hi = 1, height_count21    height = heights(hi)
    values this step1hi
  11. height ← 3

    20do hi = 1, height_count21    height = heights(hi)22    area = width * height
    values this step3height
  12. area ← 6

    21height = heights(hi)22area = width * height23if (area > best_area) then
    values this step6area2width3height
  13. if (area > best_area) then

    22area = width * height23if (area > best_area) then24    best_area = area
    values this step.true.area > best_area
  14. best_area ← 6

    23if (area > best_area) then24    best_area = area25    best_width = width
    values this step6best_area
  15. best_width ← 2

    24best_area = area25best_width = width26best_height = height
    values this step2best_width
  16. best_height ← 3

    25    best_width = width26    best_height = height27end if
    values this step3best_height
  17. hi ← 2

    19width = widths(wi)20do hi = 1, height_count21    height = heights(hi)
    values this step2hi
  18. height ← 5

    20do hi = 1, height_count21    height = heights(hi)22    area = width * height
    values this step5height
  19. area ← 10

    21height = heights(hi)22area = width * height23if (area > best_area) then
    values this step10area2width5height
  20. if (area > best_area) then

    22area = width * height23if (area > best_area) then24    best_area = area
    values this step.true.area > best_area
  21. best_area ← 10

    23if (area > best_area) then24    best_area = area25    best_width = width
    values this step10best_area
  22. best_width ← 2

    24best_area = area25best_width = width26best_height = height
    values this step2best_width
  23. best_height ← 5

    25    best_width = width26    best_height = height27end if
    values this step5best_height
  24. wi ← 2

    17best_height = 018do wi = 1, width_count19    width = widths(wi)
    values this step2wi
  25. width ← 4

    18do wi = 1, width_count19    width = widths(wi)20    do hi = 1, height_count
    values this step4width
  26. hi ← 1

    19width = widths(wi)20do hi = 1, height_count21    height = heights(hi)
    values this step1hi
  27. height ← 3

    20do hi = 1, height_count21    height = heights(hi)22    area = width * height
    values this step3height
  28. area ← 12

    21height = heights(hi)22area = width * height23if (area > best_area) then
    values this step12area4width3height
  29. if (area > best_area) then

    22area = width * height23if (area > best_area) then24    best_area = area
    values this step.true.area > best_area
  30. best_area ← 12

    23if (area > best_area) then24    best_area = area25    best_width = width
    values this step12best_area
  31. best_width ← 4

    24best_area = area25best_width = width26best_height = height
    values this step4best_width
  32. best_height ← 3

    25    best_width = width26    best_height = height27end if
    values this step3best_height
  33. hi ← 2

    19width = widths(wi)20do hi = 1, height_count21    height = heights(hi)
    values this step2hi
  34. height ← 5

    20do hi = 1, height_count21    height = heights(hi)22    area = width * height
    values this step5height
  35. area ← 20

    21height = heights(hi)22area = width * height23if (area > best_area) then
    values this step20area4width5height
  36. if (area > best_area) then

    22area = width * height23if (area > best_area) then24    best_area = area
    values this step.true.area > best_area
  37. best_area ← 20

    23if (area > best_area) then24    best_area = area25    best_width = width
    values this step20best_area
  38. best_width ← 4

    24best_area = area25best_width = width26best_height = height
    values this step4best_width
  39. best_height ← 5

    25    best_width = width26    best_height = height27end if
    values this step5best_height
  40. wi ← 3

    17best_height = 018do wi = 1, width_count19    width = widths(wi)
    values this step3wi
  41. width ← 6

    18do wi = 1, width_count19    width = widths(wi)20    do hi = 1, height_count
    values this step6width
  42. hi ← 1

    19width = widths(wi)20do hi = 1, height_count21    height = heights(hi)
    values this step1hi
  43. height ← 3

    20do hi = 1, height_count21    height = heights(hi)22    area = width * height
    values this step3height
  44. area ← 18

    21height = heights(hi)22area = width * height23if (area > best_area) then
    values this step18area6width3height
  45. if (area > best_area) then

    22area = width * height23if (area > best_area) then24    best_area = area
    values this step.false.area > best_area
  46. hi ← 2

    19width = widths(wi)20do hi = 1, height_count21    height = heights(hi)
    values this step2hi
  47. height ← 5

    20do hi = 1, height_count21    height = heights(hi)22    area = width * height
    values this step5height
  48. area ← 30

    21height = heights(hi)22area = width * height23if (area > best_area) then
    values this step30area6width5height
  49. if (area > best_area) then

    22area = width * height23if (area > best_area) then24    best_area = area
    values this step.true.area > best_area
  50. best_area ← 30

    23if (area > best_area) then24    best_area = area25    best_width = width
    values this step30best_area
  51. best_width ← 6

    24best_area = area25best_width = width26best_height = height
    values this step6best_width
  52. best_height ← 5

    25    best_width = width26    best_height = height27end if
    values this step5best_height
  53. print '(I0, 1X, I0, 1X, I0)', best_width, best_height, best_area

    29    end do30    print '(I0, 1X, I0, 1X, I0)', best_width, best_height, best_area31end program nested_sweep_area_demo
    output6 5 30
    values this step6best_width5best_height30best_area
nested loop The outer loop picks one width and the inner loop tries every height.
combination Each `(width, height)` pair is one sweep case.
best combination The current best parameter pair is updated when its area is larger.