Search code examples
fortrangfortran

Sum of certain elements in an array in Fortran


I have an array like this :

1717.531
1717.364
1716.725
1716.723
1716.536
1716.304
1716.221
1715.998
1715.799
1715.702
1715.648
1715.254
1715.186
1714.733
1714.532
1714.266
1714.223
1714.094
1713.943
1713.873
1713.803
1713.578

I want to SUM each 5 elements separately, which function to use and how?


Solution

  • You could use a loop with a step size:

    do i=1, N, 5
    

    Then the intrinsic function sum applied to slices of the array:

    sum (a(i: i+4))