函数不应该在C++

发布于 2025-02-08 19:36:24 字数 539 浏览 1 评论 0原文

我正在尝试制作自己的功能,该功能采用双阵列并返回最大索引。但是,当我运行代码以在下面的随机数组中检查函数时,它应该返回9时返回0。在main()中使用的代码有什么问题,但在函数中使用时不使用?

int findMaxIndex(double array[]) {
    int maxIndex=0;
    int arraySize = sizeof(array)/sizeof(array[0]);
    for (int i=0;i<arraySize;i++) {
        if (array[maxIndex]<array[i]) {
            maxIndex=i;
        }
    }
    return maxIndex;
}

int main() {
    double array[10] = 
{11.20,22.2,33.3,43.4,55.5,66.6,77.7,88.8,90.0,100};

    cout <<findMaxIndex(array)<<endl;

    return 0;
}

I am trying to make my own function that takes a double array and returns maximum index. But when I run the code to check the function with a random array below, it returns 0 when it's supposed to return 9. When I run the same exact code inside the function in main, it works. What's wrong with my code that is working in main() but not when used inside a function?

int findMaxIndex(double array[]) {
    int maxIndex=0;
    int arraySize = sizeof(array)/sizeof(array[0]);
    for (int i=0;i<arraySize;i++) {
        if (array[maxIndex]<array[i]) {
            maxIndex=i;
        }
    }
    return maxIndex;
}

int main() {
    double array[10] = 
{11.20,22.2,33.3,43.4,55.5,66.6,77.7,88.8,90.0,100};

    cout <<findMaxIndex(array)<<endl;

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文