Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 143

 
fxsaber:
Ran it as is.
template <typename T>
int Strange( const T &InArray[] )
{
  int Res = 1;  
  
  T Array[];
  
  const int Size = ArraySize(InArray);
  
  if ((ArrayCopy(Array, InArray) == Size) && ArraySort(Array))
  {    
    int Tmp = 1;
    
    ArrayPrint(Array);
    
    for (int i = 1; i < Size; i++)
    {
      if (Array[i - 1] != Array[i])
      {
        if (Tmp > Res)
          Res = Tmp;
        
        Tmp = 0;
      }
        
      Tmp++;
    }
  }

  return(Res);
}

void OnStart()
{
  int Array[] = {1, 2, 3, 1, 2, 1, 2, 2};
  
  Print(Strange(Array));
}
It works.

This is what he gives out:

2017.03.07 14:42:44.141 1 1 1 2 2 2 2 3
2017.03.07 14:42:44.141 4

The top line is the sorted input array, the bottom line is the result of Strange().

If we look carefully at the sorted array, and mark with colour the matches, and then count the number of numbers more than one with the same colour, we get the result:

1 1 1 2 2 2 2 3- total , you should have returned 2

And the function returned 4. Although - also progress - with me it always returned 1 - how so - did not understand.

 
Artyom Trishkin:

1 1 1 2 2 22 3 - in total , it should have returned 2

And the function returned 4. Although - also progress - with me it always returned 1 - how so - did not understand.

Do you need to return the number or the most frequent element? If the second, then
template <typename T>
int Strange( const T &InArray[] )
{
  int Res = 0;
  
  T Array[];
  
  const int Size = ArraySize(InArray);
  
  if ((ArrayCopy(Array, InArray) == Size) && ArraySort(Array))
  {    
    int Max = 0;
    int Tmp = 0;
    
    ArrayPrint(Array);
    
    for (int i = 0; i < Size - 1; i++)
    {
      Tmp++;
      
      if ((Array[i] != Array[i + 1]) || (i == Size - 2))
      {
        if (Tmp > Max)
        {
          Max = Tmp;
          
          Res = Array[i];
        }
        
        Tmp = 0;
      }        
    }
  }

  return(Res);
}

void OnStart()
{
  int Array[] = {1, 2, 3, 1, 2, 1, 2, 2, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3};
  
  Print(Strange(Array));
}
 
fxsaber:
Do you want to return the number or the most frequent item?

The number of different takes.

What does your function return? The highest number of matches? That's a useful thing too...

 
Artyom Trishkin:

The number of different takes.

You formulate it strangely. "0, 0, 1, 1, 2, 2" - 3?

What does your function return? The highest number of matches?

Yes.
 
fxsaber:

You formulate it strangely. "0, 0, 1, 1, 2, 2" - 3?

Yes - three matches of three different numbers.
 
Artyom Trishkin:
Yes - three matches of three different numbers.
template <typename T>
int Strange( const T &InArray[] )
{
  int Res = 0;  
  
  T Array[];
  
  const int Size = ArraySize(InArray);
  
  if ((ArrayCopy(Array, InArray) == Size) && ArraySort(Array))
  {    
    int Tmp = 1;
    
    ArrayPrint(Array);
    
    for (int i = 1; i < Size; i++, Tmp++)
      if (Array[i - 1] != Array[i])
      {
        if (Tmp > 1)
          Res++;
        
        Tmp = 0;
      }
      
    if (Tmp > 1)    
      Res++;
  }

  return(Res);
}

void OnStart()
{
  int Array[] = {1, 2, 3, 1, 2, 1, 2, 2, 3, 4, 4};
  
  Print(Strange(Array));
}
 
fxsaber:
So do you want to return the number or the most frequent item? If the latter, then
template <typename T>
int Strange( const T &InArray[] )
{
  int Res = 0;
  
  T Array[];
  
  const int Size = ArraySize(InArray);
  
  if ((ArrayCopy(Array, InArray) == Size) && ArraySort(Array))
  {    
    int Max = 0;
    int Tmp = 0;
    
    ArrayPrint(Array);
    
    for (int i = 0; i < Size - 1; i++)
    {
      Tmp++;
      
      if ((Array[i] != Array[i + 1]) || (i == Size - 2))
      {
        if (Tmp > Max)
        {
          Max = Tmp;
          
          Res = Array[i];
        }
        
        Tmp = 0;
      }        
    }
  }

  return(Res);
}

void OnStart()
{
  int Array[] = {1, 2, 3, 1, 2, 1, 2, 2, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3};
  
  Print(Strange(Array));
}

Cool. But a little different - it's a template function, right:

template <typename T>
T Strange( const T &InArray[] )
{
  T Res = 0;
  
  T Array[];
  
  const int Size = ArraySize(InArray);
  
  if ((ArrayCopy(Array, InArray) == Size) && ArraySort(Array))
  {    
    int Max = 0;
    int Tmp = 0;
    
    ArrayPrint(Array);
    
    for (int i = 0; i < Size - 1; i++)
    {
      Tmp++;
      
      if ((Array[i] != Array[i + 1]) || (i == Size - 2))
      {
        if (Tmp > Max)
        {
          Max = Tmp;
          
          Res = Array[i];
        }
        
        Tmp = 0;
      }        
    }
  }

  return(Res);
}

void OnStart()
{
  double Array[] = {1, 2, 3, 1, 2, 1, 2, 2, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3};
  
  Print(Strange(Array));
}
 
fxsaber:
template <typename T>
int Strange( const T &InArray[] )
{
  int Res = 0;  
  
  T Array[];
  
  const int Size = ArraySize(InArray);
  
  if ((ArrayCopy(Array, InArray) == Size) && ArraySort(Array))
  {    
    int Tmp = 1;
    
    ArrayPrint(Array);
    
    for (int i = 1; i < Size; i++)
    {
      if (Array[i - 1] != Array[i])
      {
        if (Tmp > 1)
          Res++;
        
        Tmp = 0;
      }
        
      Tmp++;
    }
  }

  return(Res);
}

void OnStart()
{
  int Array[] = {1, 2, 3, 1, 2, 1, 2, 2};
  
  Print(Strange(Array));
}

What does this one return?

If you make the array different, it's not correct.

template <typename T>
int Strange( const T &InArray[] )
{
  int Res = 0;  
  
  T Array[];
  
  const int Size = ArraySize(InArray);
  
  if ((ArrayCopy(Array, InArray) == Size) && ArraySort(Array))
  {    
    int Tmp = 1;
    
    ArrayPrint(Array);
    
    for (int i = 1; i < Size; i++)
    {
      if (Array[i - 1] != Array[i])
      {
        if (Tmp > 1)
          Res++;
        
        Tmp = 0;
      }
        
      Tmp++;
    }
  }

  return(Res);
}

void OnStart()
{
  int Array[] = {1, 2, 3, 1, 2, 1, 2, 2, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3};
  
  Print(Strange(Array));
}

It returns.

2017.03.07 15:48:26.985 1 1 1 1 1 1 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3
2017.03.07 15:48:26.985 2

Is it exactly looking for the number of matching numbers in the array? Or maybe you've done something different, and I'll be looking for an error

 

I'm already confused)

You have an array:

Array[] = {1, 2, 3, 1, 2, 1, 2, 2, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3};

What should the function eventually return and why? The number of matches for each number, or a specific maximum number, or ... Did I write something, but it must be wrong, and wrong?

 
Artyom Trishkin:

Is it exactly looking for the number of matching numbers in the array? Or maybe you've done something else, and I'll be looking for an error.

You checked the old code. Double-check it.
Reason: