数组中的二进制搜索

有人可以更正并完成以下代码吗?我做不到...

我首先想要一个从1到20的数组生成的随机数。

程序必须通过在不同阶段猜测数组的中间数来找到随机数,并在每次循环后消除剩余数的一半。

假设随机数是13

由于数组在1到20之间,因此第一个猜测数字为10,因为这是数组中间的数字。

由于猜测数10低于随机数13,因此下一个测试为15(对应于(20 +10)/ 2)。

由于猜测数15高于随机数13,因此下一个测试为12(对应于(15 +10)/ 2)。

由于猜测数12低于随机数13,因此下一个测试为13(对应于(12 + 15)/ 2)。

猜数现在与随机数匹配

有我的代码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {

    srand(time(NULL));
    int array [20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15,16,17,18,19,20};
    int randomIndex = rand() % 20;
    int randomValue = array[randomIndex];
    int low = 0;
    int high = 20;
    int middle = (low + high) / 2;


    printf("The random number to find is %d\n", randomValue);

    while (middle <= randomValue) {

        if (middle < randomValue) {
            printf("The number %d is lower than the random number\n", middle);

        }

        if (middle > randomValue) {
            printf("The number %d is lower than the random number\n", middle);
        }

        if (middle == randomValue) {
            printf("The number %d  is the correct random number", middle);
        }

    }
    return 0;

}

并且有预期的输出

预期输出(以13为随机数):

The number 10 is lower than the random number

The number 15 is higher than the random number

The number 12 is lower than the random number

The number 13 is the correct random number

我努力奋斗了几个小时。

任何帮助将不胜感激。在此先感谢您。

编辑:每个语句的循环中变量“ low”和“ high”的值应该是多少?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {

    srand(time(NULL));
    int array [20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15,16,17,18,19,20};
    int randomIndex = rand() % 19;
    int randomValue = array[randomIndex];
    int low = 0;
    int high = 19;
    int middle = (low + high) / 2;


    printf("The random number to fine is %d\n", randomValue);

    while (middle <= randomValue) {

        if (middle < randomValue) {
            printf("The number %d is lower than the random number\n", middle);
            low  = ;
            high = ;
            middle = (low + high) / 2;
        }

        if (middle > randomValue) {
            printf("The number %d is lower than the random number\n", middle);
            low  = ;
            high = ;
            middle = (low + high) / 2;

        }

        if (middle == randomValue) {
            printf("The number %d  is the correct random number", middle);
        }

    }
    return 0;

}