Это часть кода, которая выполняет бинарный поиск в массиве.
int arrayBinary_search(myarray[], key){
int selector = 0;
int low_limit = 0;
int high_limit = SIZE;
while (1){
selector = (low_limit+high_limit)/2;
printf("The selector is: %d\n", selector);
if (myarray[selector] == key){
return 1;
}
else {
if (low_limit==selector || high_limit==selector) // this is the condition
break;
if (key < myarray[selector])
high_limit = selector;
else
low_limit = selector;
printf("The high_limit is: %d\n", high_limit);
printf("The low_limit is: %d\n", low_limit);
}
}
}
Код работает, но так как не рекомендуется ставить while (1), я хотел вставить условие low_limit==selector || high_limit==selector
непосредственно внутри условия while, заменяющего это «1».
Итак, это должно быть:
int arrayBinary_search(myarray[], key){
int selector = 0;
int low_limit = 0;
int high_limit = SIZE;
while (!(low_limit==selector) && !(high_limit==selector)){ // this is the condition implemented
selector = (low_limit+high_limit)/2;
printf("The selector is: %d\n", selector);
if (myarray[selector] == key){
return 1;
}
else {
//if (low_limit==selector || high_limit==selector)
// break;
if (key < myarray[selector])
high_limit = selector;
else
low_limit = selector;
printf("The high_limit is: %d\n", high_limit);
printf("The low_limit is: %d\n", low_limit);
}
}
}
Поскольку это условие должно соответствовать ОТКЛЮЧЕННОМУ ИЛИ, то есть двум отрицательным элементам с AND.
Но это не работает.
Полный код здесь: https://hastebin.com/cebaxicasu.cpp