报错(写二分查找的代码)

有谁知道为什么报错吗
代码:

#include <iostream>
using namespace std;

int len(double arr[])
{
    return sizeof(arr) / sizeof(double);
}

int bin_search(double arr[], double val)
{
    int low = 0;
    int high = len(arr) - 1;
    int mid = (low + high) / 2;
    while (low <= high)
        double guess = arr[mid];
        if (val < guess)
            high = mid;
        else if (val > guess)
            low = mid;
        else
            cout << "Found the value" << endl;
            return 0;
        mid = (low + high) / 2;
    cout << "The value is not found" << endl;
    return -1;
}

int main()
{
    double arr[] = { 1, 2, 3, 4, 5 };
    bin_search(arr, 3);
    bin_search(arr, 7);
    return 0;
}

报错:

> Executing task: C/C++: g++.exe 生成活动文件 <

正在启动生成...
C:\msys64\mingw64\bin\g++.exe -fdiagnostics-color=always -g C:\Users\Ying\Desktop\C++\其他\算法\二分查找.cpp -o C:\Users\Ying\Desktop\C++\其他\算法\二分查找.exe
C:\Users\Ying\Desktop\C++\����\�㷨\���ֲ���.cpp: In function 'int len(double*)':
C:\Users\Ying\Desktop\C++\����\�㷨\���ֲ���.cpp:6:23: warning: 'sizeof' on array function parameter 'arr' will return size of 'double*' [-Wsizeof-array-argument]
    6 |         return sizeof(arr) / sizeof(double);
      |                      ~^~~~
C:\Users\Ying\Desktop\C++\����\�㷨\���ֲ���.cpp:4:16: note: declared here
    4 | int len(double arr[])
      |         ~~~~~~~^~~~~
C:\Users\Ying\Desktop\C++\����\�㷨\���ֲ���.cpp: In function 'int bin_search(double*, double)':
C:\Users\Ying\Desktop\C++\����\�㷨\���ֲ���.cpp:16:27: error: 'guess' was not declared in this scope
   16 |                 if (val < guess)
      |                           ^~~~~

生成已完成,但出现错误。
终端进程已终止,退出代码: -1。

终端将被任务重用,按任意键关闭。

int len(double arr[])
{
return sizeof(arr) / sizeof(double);
}
这么计算数组大小是错误的,当数组作为函数参数时,实际是作为指针传递的,sizeof(arr)得到的是指针的大小,不是数组的大小。
所以bin_search函数还需要一个参数,表示数组的大小


#include <iostream>
using namespace std;
 
int len(double arr[])
{
    return sizeof(arr) / sizeof(double);
}
 
int bin_search(double arr[], double val)
{
    int low = 0;
    int high = len(arr) - 1;
    int mid = (low + high) / 2;
    while (low <high)
    {
        double guess = arr[mid];
        if (val < guess)
            high = mid-1;
        else if (val > guess)
            low = mid+1;
        else
        {
            cout << "Found the value" << endl;
            return 0;
        }
        mid = (low + high) / 2;
     }
    cout << "The value is not found" << endl;
    return -1;
}
 
int main()
{
    double arr[] = { 1, 2, 3, 4, 5 };
    bin_search(arr, 3);
    bin_search(arr, 7);
    return 0;
}

直接把数组长度在主函数确定 int len=sizeof(arr)/sizeof(double);