Array Help

 

Hello,

I have got an array of [1,2,3,4,5, ...]

How do I change it to [...,5,4,3,2,1] ?

Any help is appreciated. Thanks.

 

Something like this (not verified...):

void ArrayFlip( int& arr[] )
{
   int temp, len=ArraySize(arr);
   if (len<2) return;
   for(int i=0; i<len/2; i++) {
      temp=arr[i];
      arr[i]=arr[len-1-i];
      arr[len-1-i]=temp;
   }
}
 

gordon is it possible to use ArraySetAsSeries to simply change the indexing direction? Does it work like that? I've used it before but perhaps I was using it in error (never had much confidence in my grasp of this particular function).

 
I don't remember ever using that function. But yeah, seems to do the job. It also does it much faster. Good one, phillip.
 
1005phillip:

gordon is it possible to use ArraySetAsSeries to simply change the indexing direction? Does it work like that? I've used it before but perhaps I was using it in error (never had much confidence in my grasp of this particular function).

#property show_inputs  // This Works...

extern int Bar_Limit = 5;
extern bool Is_Series = false;

double Open_Val[];
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
   ArrayResize(Open_Val, Bar_Limit);
   ArrayInitialize(Open_Val,0.0);
   
   for(int i = Bar_Limit-1; i >= 0; i --)
   {
      Open_Val[i] = Open[i];
   }
   
   ArraySetAsSeries(Open_Val, Is_Series);
   for( i = Bar_Limit-1; i >= 0; i --)
   {
      Alert (" Open_Val[",i,"] = ", Open_Val[i] );
   }
   
   if(Is_Series) string S_Series = "TRUE"; else S_Series = "FALSE";
   Alert (" END OF Is_Series ", S_Series," test ");
   
   return(0);
  }
//+------------------------------------------------------------------+

 If you put ArraySetAsSeries just after ArrayInitalize then the script shows no difference in Open_Val. I only got it after the third try... hth.
Added : i.e. This function works for calling ( not filling ) an array  values in a reversed  order. This is what kept it confusing for me in the past.

Reason: