Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C While条件仅当我放置While 1并放置if和break时才受尊重_C_Arrays_Sorting - Fatal编程技术网

C While条件仅当我放置While 1并放置if和break时才受尊重

C While条件仅当我放置While 1并放置if和break时才受尊重,c,arrays,sorting,C,Arrays,Sorting,这是在数组中执行二进制搜索的代码的一部分 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);

这是在数组中执行二进制搜索的代码的一部分

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

    }

}
因为这个条件应该对应一个否定的OR,它是两个带AND的否定项

但是,它不起作用


此处的完整代码:

变量选择器的值初始化如下:

 int selector = 0;
因此,您的程序将永远不会进入while循环,因为,
!(下限==选择器)
始终为false

以下更改将使代码正常工作

int arrayBinary_search(int myarray[], int key){
    int selector;
    int low_limit = 0;
    int high_limit = SIZE;
    while (low_limit <= high_limit){
        selector = (low_limit+ high_limit)/2;
        printf("The selector is: %d\n", selector);
        if (myarray[selector] == key){
            return 1;
        }
        else {
            if (key < myarray[selector])
                high_limit = selector-1;
            else
                low_limit = selector+1;
            printf("The high_limit is: %d\n", high_limit);
            printf("The low_limit is: %d\n", low_limit);
        }

    }

}

arrayBinary\u search
的函数定义没有参数类型,只有参数标识符,这本身就是一个编译器错误。@roberts-supportsmonicacellio实际上编译器给我的是警告而不是错误。顺便说一句,你是对的,我必须添加类型我不知道你使用哪个编译器,但gcc将其视为错误,没有任何附加标志。顺便说一句,除非你100%地知道编译器警告是多余的(但这种情况很少发生)。我现在用clang试过了,它确实只给出了关于这一点的警告-很有趣-所以我猜你是用clang编译的。无论如何,您都不能忽略函数参数类型。。。。或者,一个
do{…}while()
循环可能更合适,这样在第一次迭代之前不会计算循环条件。您是对的,但是。。。这个程序没有像我预期的那样工作。因为是,现在它进入循环,但它只执行一个循环,因为它会立即阻塞,因为“选择器”在第一个循环后具有相同的“高限制”值。需要以下while循环“while(低限制
int arrayBinary_search(int myarray[], int key){
    int selector;
    int low_limit = 0;
    int high_limit = SIZE;
    while (low_limit <= high_limit){
        selector = (low_limit+ high_limit)/2;
        printf("The selector is: %d\n", selector);
        if (myarray[selector] == key){
            return 1;
        }
        else {
            if (key < myarray[selector])
                high_limit = selector-1;
            else
                low_limit = selector+1;
            printf("The high_limit is: %d\n", high_limit);
            printf("The low_limit is: %d\n", low_limit);
        }

    }

}
int arrayGenerator(int myarray[]);
int arrayBinary_search(int myarray[], int key);