为什么 Dev C++在运行整个代码来查找 10 个元素之间的最大和最小数字之前停止工作吗?

发布于 2025-01-16 20:39:45 字数 1447 浏览 3 评论 0原文

这是我编写的用于查找 10 个元素之间的最大和最小数字的代码:

#include <iostream>
using namespace std;

#define SIZE 10
void acceptNumbers (int Numbers[SIZE]);
int High(int Numbers[SIZE]);
int Low(int Numbers[SIZE]);

void acceptNumbers(int Numbers[SIZE])
{int i, *max, *min;
    for (i=0; i<SIZE; i++)
    cin>>Numbers[i];

    max = Numbers;
    min = Numbers;
}

int High(int Numbers[SIZE])
{int i, *max;
    for (i = 0; i < SIZE; i++)
    {
        if (*(Numbers+i) > *max)
            max = (Numbers+i);
    }
    cout<<*max;
}

int Low(int Numbers[SIZE])
{int i, *min;
    for (i = 0; i < SIZE; i++)
    {
        if (*(Numbers+i) < *min)
        min = (Numbers+i);
    }
    cout<<*min;
}

int main()
{
    int arr[SIZE];

    cout<<"Enter Elements: \n";
    acceptNumbers(arr);

    cout<<"Highest Number: ";
    High(arr);
    cout<<endl;

    cout<<"Lowest Number: ";
    Low(arr);
    cout<<endl;
    }

在我的 PC 上,它不会运行整个程序。当它到达查找最大和最小数字的代码时它就停止了 输入图片这里的描述

这是我正在做的作业,我不能使用任何快捷方式和诸如此类的东西,因为给出了需要使用的代码行。因此,我尝试在另一台电脑上运行此代码,效果很好:

在此处输入图像描述

我也尝试在在线 c++ 编译器上运行它,但它仍然不起作用。我在网上找不到解决方案,所以这是我最后的手段,请帮助我

This is the code that I made to find the highest and lowest number between 10 elements:

#include <iostream>
using namespace std;

#define SIZE 10
void acceptNumbers (int Numbers[SIZE]);
int High(int Numbers[SIZE]);
int Low(int Numbers[SIZE]);

void acceptNumbers(int Numbers[SIZE])
{int i, *max, *min;
    for (i=0; i<SIZE; i++)
    cin>>Numbers[i];

    max = Numbers;
    min = Numbers;
}

int High(int Numbers[SIZE])
{int i, *max;
    for (i = 0; i < SIZE; i++)
    {
        if (*(Numbers+i) > *max)
            max = (Numbers+i);
    }
    cout<<*max;
}

int Low(int Numbers[SIZE])
{int i, *min;
    for (i = 0; i < SIZE; i++)
    {
        if (*(Numbers+i) < *min)
        min = (Numbers+i);
    }
    cout<<*min;
}

int main()
{
    int arr[SIZE];

    cout<<"Enter Elements: \n";
    acceptNumbers(arr);

    cout<<"Highest Number: ";
    High(arr);
    cout<<endl;

    cout<<"Lowest Number: ";
    Low(arr);
    cout<<endl;
    }

On my PC, it doesn't run the whole program. It just stops right when it reaches the code for finding the highest and lowest number
enter image description here

This is homework I'm working on and I can't use any shortcuts and whatnots since there is a required line of code to be used that is given. And so I tried running this code on another PC and it worked just fine:

enter image description here

I also tried running it on online c++ compiler and it still doesn't work. I can't find solution online so this is my last resort please help me

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怎会甘心 2025-01-23 20:39:45

您的代码表现出未定义的行为

在每个函数中,您将 minmax 声明为局部变量。因此,当您在 acceptNumbers() 中初始化 min/max 时,它们仅在该函数中初始化。您根本没有初始化 High()/Low() 中的 min/max 变量。当这些函数尝试取消引用那些未初始化的变量时,您的代码就会崩溃。

此外,您的 High()/Low() 函数具有非 void 返回类型,但实际上并不是 return'ing任何值。

试试这个:

#include <iostream>
using namespace std;

#define SIZE 10
void acceptNumbers (int Numbers[SIZE]);
int High(int Numbers[SIZE]);
int Low(int Numbers[SIZE]);

void acceptNumbers(int Numbers[SIZE])
{
    for (int i=0; i<SIZE; i++)
        cin>>Numbers[i];
}

int High(int Numbers[SIZE])
{
    int *max = Numbers;
    for (int i = 1; i < SIZE; i++)
    {
        if (*(Numbers+i) > *max)
            max = (Numbers+i);
    }
    return *max;
}

int Low(int Numbers[SIZE])
{
    int *min = Numbers;
    for (int i = 1; i < SIZE; i++)
    {
        if (*(Numbers+i) < *min)
            min = (Numbers+i);
    }
    return *min;
}

int main()
{
    int arr[SIZE];

    cout<<"Enter Elements: \n";
    acceptNumbers(arr);

    cout<<"Highest Number: ";
    cout<<High(arr);
    cout<<endl;

    cout<<"Lowest Number: ";
    cout<<Low(arr);
    cout<<endl;
}

Your code exhibits undefined behavior.

In each of your functions, you are declaring min and max as local variables. Thus, when you initialize min/max in acceptNumbers(), they are only initialized in that function. You are not initializing the min/max variables in High()/Low() at all. Your code is crashing when those functions try to dereference those uninitialized variables.

Also, your High()/Low() functions have non-void return types, but are not actually return'ing any values.

Try this instead:

#include <iostream>
using namespace std;

#define SIZE 10
void acceptNumbers (int Numbers[SIZE]);
int High(int Numbers[SIZE]);
int Low(int Numbers[SIZE]);

void acceptNumbers(int Numbers[SIZE])
{
    for (int i=0; i<SIZE; i++)
        cin>>Numbers[i];
}

int High(int Numbers[SIZE])
{
    int *max = Numbers;
    for (int i = 1; i < SIZE; i++)
    {
        if (*(Numbers+i) > *max)
            max = (Numbers+i);
    }
    return *max;
}

int Low(int Numbers[SIZE])
{
    int *min = Numbers;
    for (int i = 1; i < SIZE; i++)
    {
        if (*(Numbers+i) < *min)
            min = (Numbers+i);
    }
    return *min;
}

int main()
{
    int arr[SIZE];

    cout<<"Enter Elements: \n";
    acceptNumbers(arr);

    cout<<"Highest Number: ";
    cout<<High(arr);
    cout<<endl;

    cout<<"Lowest Number: ";
    cout<<Low(arr);
    cout<<endl;
}
谈下烟灰 2025-01-23 20:39:45

函数 Low() 和 High() 仅声明 int* max、min 但从未初始化。尽管他们持有无效地址。

if (*(Numbers + i) > *max)

此函数会导致分段错误,因为您的程序尝试取消引用无效的内存地址。

如果您想存储int *min, *max,请将它们设置为全局或在 main() 中声明它们。

The functions Low() and High() only declare the int* max, min but never initialize. Though they hold invalid addresses.

if (*(Numbers + i) > *max)

This function causes a segmentation fault because your program tries to dereference an invalid memory address.

If you want to store int *min, *max make them either global or declare them in main().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文