Cualquier pregunta de los recién llegados sobre MQL4 y MQL5, ayuda y discusión sobre algoritmos y códigos - página 143

 
fxsaber:
Lo he hecho tal cual.
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));
}
Funciona.

Esto es lo que da:

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

La línea superior es la matriz de entrada ordenada, la línea inferior es el resultado de Strange().

Si observamos detenidamente la matriz ordenada, y marcamos con color las coincidencias, y luego contamos el número de números más de uno con el mismo color, obtenemos el resultado:

1 1 1 2 2 2 3- total , debería haber devuelto 2

Y la función devolvió 4. Aunque - también el progreso - conmigo siempre volvió 1 - cómo así - no entendía.

 
Artyom Trishkin:

1 1 1 2 22 3 - en total , debería haber devuelto 2

Y la función devolvió 4. Aunque - también el progreso - conmigo siempre volvió 1 - cómo así - no entendía.

¿Necesita devolver el número o el elemento más frecuente? Si el segundo, entonces
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:
¿Quiere devolver el número o el elemento más frecuente?

El número de tomas diferentes.

¿Qué devuelve su función? ¿El mayor número de partidos? Eso también es algo útil...

 
Artyom Trishkin:

El número de tomas diferentes.

Lo formula de forma extraña. "0, 0, 1, 1, 2, 2" - 3?

¿Qué devuelve su función? ¿El mayor número de partidos?

Sí.
 
fxsaber:

Lo formula de forma extraña. "0, 0, 1, 1, 2, 2" - 3?

Sí, tres partidos de tres números diferentes.
 
Artyom Trishkin:
Sí, tres partidos de tres números diferentes.
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:
Entonces, ¿quieres devolver el número o el elemento más frecuente? Si es esto último, entonces
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));
}

Genial. Pero un poco diferente - es una función de la plantilla, a la derecha:

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));
}

¿Qué devuelve éste?

Si haces la matriz diferente, no es correcto.

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));
}

Vuelve.

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

¿Busca exactamente el número de números coincidentes en la matriz? O tal vez usted ha hecho algo diferente, y voy a buscar un error

 

Ya estoy confundido)

Tienes una matriz:

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

¿Qué debería devolver la función finalmente y por qué? El número de aciertos para cada número, o un número máximo específico, o ... ¿He escrito algo, pero debe estar mal, y mal?

 
Artyom Trishkin:

¿Busca exactamente el número de números coincidentes en la matriz? O tal vez haya hecho algo más, y buscaré un error.

Has comprobado el código antiguo. Vuelve a comprobarlo.
Razón de la queja: