Please help,

 

Hi guys, I tried to get a median from an array, but I got 0, I don't know where it's wrong, could you help and correct this code please ?, thank you

//========================================================================

double median,array1[];

avr1 = 11.45;

avr2 = 4.6;

avr3 = 7.781254;

avr4 = 10.68211;

avr5 = 12.21;

avr6 = 11;

avr7 = 8;

avr8 = 20;

//only 8 numbers in this array.

array1[0] = avr1; array1[1] = avr2; array1[2] = avr3; array1[3] = avr4; array1[4] = avr5; array1[5] = avr6; array1[6] = avr7; array1[7] = avr8;

ArraySort(array1,WHOLE_ARRAY,0,MODE_DESCEND);

median=(array1[8/2]+array1[(8/2)-1])/2.0;

//================================================================================

 
 

Hi, thanks, I tried to add this to the code but it doesn't work, could you please show me how to use this function ?


//========================================================================

double median,array1[];

avr1 = 11.45;

avr2 = 4.6;

avr3 = 7.781254;

avr4 = 10.68211;

avr5 = 12.21;

avr6 = 11;

avr7 = 8;

avr8 = 20;

//only 8 numbers in this array.

ArrayResize(array1,8) ;

array1[0] = avr1; array1[1] = avr2; array1[2] = avr3; array1[3] = avr4; array1[4] = avr5; array1[5] = avr6; array1[6] = avr7; array1[7] = avr8;

ArraySort(array1,WHOLE_ARRAY,0,MODE_DESCEND);

median=(array1[8/2]+array1[(8/2)-1])/2.0;

//================================================================================

 

i hope u know what u doing because i don't

ArraySort(array1,WHOLE_ARRAY,0,MODE_DESCEND);

now array is sorted 20, 12.21, 11.45, 11, 10.68211, 8, 7.781254, 4.6 // (array1[0] = 20, array1[1] = 12.21, array1[2] = 11.45 ......................)

now

median=(array1[8/2]+array1[(8/2)-1])/2.0; 

are the same like

median=(array1[4] + array1[3]) / 2.0; 

so array[4] = (10.68211)

array[3] = (11)

11+ 10.68211 = 21.68211

21.68211 / 2.0 = 10.841055

bottom line median = 10.841055

 
BTW take a look here
 

Yes, I think I know what I'm doing.

I tried to pass the values to array, but it doesn't work, it seems it should allocate the space for numbers first, I don't know how to do that, my final goble is going to get the median number from this array.

Anyone could help ~?

 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void start(){
    //~~~~~~~~~~
    double median;
    double sum;
    double array1[8];//-----------------------------------------------Since you already know the size. Otherwise use ArrayResize().
    double avr1 = 11.45;
    double avr2 = 4.6;
    double avr3 = 7.781254;
    double avr4 = 10.68211;
    double avr5 = 12.21;
    double avr6 = 11;
    double avr7 = 8;
    double avr8 = 20;
    //~~~~~~~~~~
    array1[0] = avr1; 
    array1[1] = avr2; 
    array1[2] = avr3; 
    array1[3] = avr4; 
    array1[4] = avr5; 
    array1[5] = avr6; 
    array1[6] = avr7; 
    array1[7] = avr8;
    //~~~~~~~~~~
    for(int i=ArraySize(array1)-1;i>=0;i--){
        sum+=array1[i];//----------------------------------------------I just added all the values together and divide by 8
    }
    //~~~~~~~~~~
    if(ArraySize(array1)>0){//-----------------------------------------Always check for Zero Divide error if you're gonna use devision
        median=sum/ArraySize(array1);//--------------------------------I'm not sure if this is the type of Median you're looking for
    }
    //~~~~~~~~~~
    Print("Median="+median);//-----------------------------------------Always Print/Alert/Comment values when troubleshooting/debugging.
    //~~~~~~~~~~
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
2011.11.20 21:05:37     2011.01.02 23:00  Temporary EURUSD,M5: Median=10.71542050
90% of the issue I see with arrays around here has to do with sizing the array. It gets even more tricky when you start dealing with multi-dimensional arrays. I suggest you spent allot of time learning about array sizes and limitations before building complex systems with them. When in doubt use the print statements. Sometimes I result to writing 1 line of code and then Print(true) or Print(Value). Doing so will save you some gray hairs.
 
it works now, I'm going to learn more about array, thank you for all your help. )