Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 143
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Ran it as is.
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));
}
This is what he gives out:
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.
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.
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));
}
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...
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?
You formulate it strangely. "0, 0, 1, 1, 2, 2" - 3?
Yes - three matches of three different numbers.
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));
}
So do you want to return the number or the most frequent item? If the latter, then
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:
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));
}
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.
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 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:
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?
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.