Sum multiple elements of a large array

 

Hi,

I am looking for help with writing a small custom code (MQL4) to sum some elements (about 25 to 35 elements) of an array with 100 elements.

I tried the following code but it did not work and I'm not sure how else to go about it.
for (int iArray = ArraySize(array) - 1; iArray >= 0; iArray--) summation += array[iArray];

The only other choice I have is to sum them manually, that is, sum=var1+var2+...var35. This will work but is clumsy and I can only modify the calculation manually.

Please help, thanks.

 

I always use this kind of loop:

int i = ArraySize(array);
while(i-->0) sum+=array[i];

To check it use the debugger: https://www.metatrader5.com/en/metaeditor/help/development/debug

Code debugging - Developing programs - MetaEditor Help
  • www.metatrader5.com
MetaEditor has a built-in debugger allowing you to check a program execution step by step (by individual functions). Place breakpoints in the code...
 
coolazice: I tried the following code but it did not work and I'm not sure how else to go about it.
  1. You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your difficulty.
              No free help (2017)

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum (2018)

    We're not going to code it for you (although it could happen if you are lucky or the issue is interesting).
              No free help (2017)

  2. Was it so hard you couldn't even attempt it?
    Not tested, not compiled, just typed.
    double meanOnArray(const double& array[], int iEnd=EMPTY, int iBeg=0){
       if(iEnd == EMPTY) iEnd = ArraySize(array);
       return sumArray(array, iEnd, iBeg) /  (iEnd - iBeg);
    }
    double sumArray(const double& array[], int iEnd, int iBeg=0){
       double sum=0;
       while(iEnd > iBeg) sum += array(--iEnd);
       return sum;
    }
    Not tested, not compiled, just typed.

 
Carl Schreiber #:

I always use this kind of loop:

To check it use the debugger: https://www.metatrader5.com/en/metaeditor/help/development/debug

Thanks Carl, I will try it out this evening and let you know if it works. I am actually developing the EA on fxdreema and will insert this your line of code in a custom code block.
 
William Roeder #:
  1. You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your difficulty.
              No free help (2017)

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum (2018)

    We're not going to code it for you (although it could happen if you are lucky or the issue is interesting).
              No free help (2017)

  2. Was it so hard you couldn't even attempt it?
    Not tested, not compiled, just typed. Not tested, not compiled, just typed.

Thanks William, I will try yours too later this evening.

Not sure some of your comments would have been necessary if you read my initial request for help; I'm sure they were well addressed there.