MQL5 Language: #define SWAP - page 2

 

Full code example, if another user need for reference.

#define SWAP(a,b) {a=a+b;b=a-b;a=a-b;}
#define SORT(a,b,c) {if(a > b) SWAP(a,b); if(a > c) SWAP(a,c); if (b>c) SWAP(b,c)}

void OnStart()
{
  int Num1 = rand();
  int Num2 = rand();
  int Num3 = rand();

  SORT(Num1, Num2, Num3);
}
 
fxsaber:

Beatiful.

 

Full example (again):

#define SWAP(a, b) a ^= (b ^= (a ^= b));
#define SORT(a, b, c) {if(a > b) SWAP(a,b); if(a > c) SWAP(a,c); if (b>c) SWAP(b,c)}

void OnStart()
{
  int Num1 = rand();
  int Num2 = rand();
  int Num3 = rand();

  SORT(Num1, Num2, Num3);
}
 
Try to swap numbers in an array with the same position to see what happens...
 
Try to take out if's before the SWAP macro...
Self-regressive Median Coefficient
Self-regressive Median Coefficient
  • 2018.08.27
  • www.mql5.com
Hi all, I am trying to build an indicator that uses the following formula and returns the expected value, so I can use the same chart as indicator_...
 
Arthur Albano:
Try to take out if's before the SWAP macro...
#define SWAP(a, b) { if (a != b) VALUE(a) ^= (VALUE(b) ^= (VALUE(a) ^= VALUE(b))); }
#define SORT(a, b, c) {if(a > b) SWAP(a,b); if(a > c) SWAP(a,c); if (b>c) SWAP(b,c)}
// Partition using Lomuto partition scheme

#define VALUE(A) list[A]
int partition(int &list[],int left,int right,int pivotIndex)
  {
   int pivotValue = list[pivotIndex];
   SWAP(pivotIndex,right);
   int storeIndex = left;
   for(int i=left; i<right; i++)
      {
         if(list[i] < pivotValue)
            {
               SWAP(storeIndex,i);
               storeIndex++;
            }
      }
   SWAP(right,storeIndex);

   return storeIndex;

  }
#undef VALUE
#define VALUE(A) A