Compare one value to 100 element array? - page 2

 
Ernst Van Der Merwe:


T1 is where the array is passed to and T2 is the value to be found.

T1 and T2 are used differently, they do the same thing? In
template<typename T1,typename T2>

In the previous item, I just leave the T1 and T2 straight like that?

bool ArrayFind(const T1 &array[],const T2 value,uint begin=0,uint count=WHOLE_ARRAY)
  {

In the previous item, do I really need to write "TI &array" or should I just write the actual array name in its place?


I thought the first one was for template parameters. How will I just leave it blank?


Template parameters template<typename T1,typename T2> are not used at all in the actual function; do they have anything to do with latency other than syntax?

 
nadiawicket:
T1 and T2 are used differently, they do the same thing? In

In the previous item, I just leave the T1 and T2 straight like that?

In the previous item, do I really need to write "TI &array" or should I just write the actual array name in its place?


I thought the first one was for template parameters. How will I just leave it blank?


It depends on if the array and value are of the same type. If they are only use one typename.

template<typename T>
bool ArrayFind(const T &array[],const T value,int begin=0,int count=WHOLE_ARRAY)
  {
   int size=ArraySize(array);
   if(count==WHOLE_ARRAY)
      count=size-begin;
   if(begin+count>size)
      return(false);
   for(int i=begin;i<begin+count;i++)
      if(array[i]==value)
         return(true);
   return(false);
  }
 
Sign Mismatch error. Should I worry about it at all? Can still execute.
 

nadiawicket
:

Sign Mismatch error. Should I worry about it at all? Can still execute.


Change the unsigned ints to ints.

bool ArrayFind(const T1 &array[],const T2 value,int begin=0,int count=WHOLE_ARRAY)
 
nadiawicket:

If you would explain a little, it would help a lot because the official documentation I didn't understand and its been like 10 hours. I'm stuck at how to actually enable/use templates, the official examples I just cant figure out.

Previous item; I substitutue ti1 and ti2 for the actual values?
static INDEX     
find(const Ti1& inp[], INDEX iBeg, INDEX iEnd,
    const Ti2& value){ ...

You are pulling this one here instead of at the top of the file again like #Define. I just put the array[] index here?

Cut/paste error. Drop the static, (I originally had the function(s) inside a struct for name space control.)

Ignore the template and just call it with whatever values you are working with.
double a[] = {...}; 
double value = ...;

INDEX iValue = find(a, 0, ArraySize(a), value);


nadiawicket: Problem is the

ArrayBsearch() only returns closest value to what you are searching. I need it to return a plain yes or no.

It returns the closes value. Then check if that is the one you want, yes or no.
 
whroeder1:
Cut/paste error. Drop the static, (I originally had the function(s) inside a struct for name space control.)

Ignore the template and just call it with whatever values you are working with.


It returns the closes value. Then check if that is the one you want, yes or no.
Very clever.
 
template<typename T1,typename T2>
bool ArrayFind(const T1 &array1[],const T2 value,uint begin=0,uint count=WHOLE_ARRAY)
  {
   int size=ArraySize(actualarrayname);
   if(count==WHOLE_ARRAY)
      count=size-begin;
   if(begin+count>size)
      return(false);
   for(int i=begin;i<count;i++)
      if(op[i]==value)
         return(true);
   return(false);

Did not need to change anything in previous code. Do I need to or not? Working fine as is right now. Would you change anything from that block?

Just changed
   int size=ArraySize(actualarrayname);
To my own array name and thats it? Dont really understand how this works but its working perfect.
 
nadiawicket:

Did not need to change anything in previous code. Do I need to or not? Working fine as is right now. Would you change anything from that block?

Just changed
To my own array name and thats it? Dont really understand how this works but its working perfect.


If a different array is passed, it wont work. It's working as is.

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   double array1[]={1.2341,1.4789,1.6434,1.9487};
   double value1=1.6434;

   if(ArrayFind(array1,value1))
      Print("Value found!");

   int array2[]={31,76,94,27,438,5};
   double value2=94;

   if(ArrayFind(array2,value2,2,1))
      Print("Value found!");

   string array3[]={"31","76","94","27","438","5"};
   int value3=438;

   if(ArrayFind(array3,value3,4))
      Print("Value found!");
  }
//+------------------------------------------------------------------+
template<typename T1,typename T2>
bool ArrayFind(const T1 &array[],const T2 value,int begin=0,int count=WHOLE_ARRAY)
  {
   int size=ArraySize(array);
   if(count==WHOLE_ARRAY)
      count=size-begin;
   if((count+=begin)>size)
      return(false);
   for(int i=begin;i<count;i++)
      if(array[i]==(T1)value)
         return(true);
   return(false);
  }
2017.05.24 08:00:40.965 ArrayFind EURUSD,H1: Value found!
2017.05.24 08:00:40.965 ArrayFind EURUSD,H1: Value found!
2017.05.24 08:00:40.965 ArrayFind EURUSD,H1: Value found!
 

You could also use the array classes in the standard library to make this easier...


Here's an example of several ways to manipulate the array with search and compare.


#include <Arrays\ArrayInt.mqh>
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   CArrayInt arr1, arr2;
   
   //add some values
   for(int i=0;i<10;i++)
      arr1.Add(i);
   
   for(int i=9;i>=0;i--)
      arr2.Add(i);
   
   //find x val in array
   int findVal = 7;
   int index = arr2.SearchLinear(findVal);
   Print("Value ",findVal," is at index ",index);
   
   //compare original arrays   
   bool sameArrOrder = arr1.CompareArray(&arr2);
   
   //create temp arrays to sort so we don't disturb the order of the original arrays.
   CArrayInt temp1,temp2;
   temp1.AddArray(&arr1);
   temp2.AddArray(&arr2);
   temp1.Sort();
   temp2.Sort();
   
   //compare sorted temp arrays
   bool sameArrNotOrder = temp1.CompareArray(&temp2);
   
   if(sameArrOrder)
      Print("Array has the same values and the same order");
   else if(!sameArrOrder && sameArrNotOrder)
      Print("Array has the same values but different order");
      
      
   
  }
 

BTW (little bit off topic), is there any size limitation for arrays? This code prints out 0.

void Something()
  {   
   int Val=1234567890;
   int myArray[];

   ArrayResize(myArray,Val);
   Print("ARRSIZE: ",IntegerToString(ArraySize(myArray)));  
  }
Reason: