I am having trouble with the ArrayBsearch function ... - page 2

 
onewithzachy:

Re - run on build 409 and 419 Vista HB. The result : 9 - 10 - 8 - 0 - 0 - 9 - 11 - 10 - 11 - 8. Did you expect the result should be : 9 - 10 - 8 - 7 - 0 - 2 - 11 - 3 - 4 - 1 ?, because if it is, I should delete my previous comment then and also now I don't understand it too, which is good because I don't have to answer question :)

Think we found bug :(

int Array_A[10] = {5, 7, 3, 1, 2, 6, 9, 8, 10, 4} ;
int Array_B[15] = {2, 4, 6, 8, 10, 12, 14, 1, 3, 5, 7, 9, 11, 13, 15} ;
It's not a bug in mt4. It is a bug in YOUR code. When you sort array_b to
int Array_B[15] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
You should get 4, 6, 2, 0, 1, 5, 8, 7, 9, 3. from ArrayBSearch
when j = 4
ArrayBsearch(Array_B,Array_A[j],WHOLE_ARRAY,0,MODE_ASCEND);
ArrayBsearch(Array_B,Array_A[4],WHOLE_ARRAY,0,MODE_ASCEND);
ArrayBsearch(Array_B,         2,WHOLE_ARRAY,0,MODE_ASCEND);
1
Array_B[1] == Array_A[4] = 2
 

But if you need return of exact value and strict existence there is no -1 return code.

From text:" If the element with the specified value doesn't exist in the array, the function returns the index of the nearest smallest value of the elements between which the searched value is located."

In that case you need to write your own search task.

 

WHRoeder:
It's not a bug in mt4. It is a bug in YOUR code. When you sort array_b to

//int Array_B[15] = {2, 4, 6, 8, 10, 12, 14, 1, 3, 5, 7, 9, 11, 13, 15};
int Array_B[15] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
You should get 4, 6, 2, 0, 1, 5, 8, 7, 9, 3. from ArrayBSearch

Yes I get that value from both build 409 and 419. However, that is a sorted Array_B, and in this case phillw does not sort Array_B.

What is the use of ArrayBsearch anyway ???

 
onewithzachy:

Yes I get that value from both build 409 and 419. However, that is a sorted Array_B, and in this case phillw does not sort Array_B.

What is the use of ArrayBsearch anyway ???

  1. Don't sort - can't use Bsearch
  2. Efficient searching in sorted arrays. iBarShift probably implimented via ArrayBsearch(Time, ...)
 
WHRoeder:
  1. Don't sort - can't use Bsearch
  2. Efficient searching in sorted arrays. iBarShift probably implimented via ArrayBsearch(Time, ...)

OK get it - sort of :). I never use ArrayBsearch(), and probably don't understand the English meaning of "Binary" :(. So that will make ArrayBsearch is ArrayBinarySearch . I guess philw can not use this and have to write the code to search it manually like he finally did :(.

Thanks WHRoeder but no thanks for this.

 
RFB:

But if you need return of exact value and strict existence there is no -1 return code.

From text:" If the element with the specified value doesn't exist in the array, the function returns the index of the nearest smallest value of the elements between which the searched value is located."

In that case you need to write your own search task.


i'm looking for the exact value in an array and if not found, return -1


anyone have an example of how to do this?

 
whats1thingnow: i'm looking for the exact value in an array and if not found, return -1 anyone have an example of how to do this?
If the array is sorted,
int index = ArrayBsearch(array,value);
if(array[index] == value) return index;
return EMPTY;
Otherwise            
for(int index = ArraySize(array); index > 0;){ --index; 
   if(array[index] == value) return index;
}
return EMPTY;
Was that so hard you couldn't even try? Next time Show us your attempt (using the CODE button) and state the nature of your problem.
          No free help 2017.04.21

Or pay someone. Top of every page is the link Freelance.
          Hiring to write script - General - MQL5 programming forum 2018.05.12

Reason: