为什么 Dev C++在运行整个代码来查找 10 个元素之间的最大和最小数字之前停止工作吗?
这是我编写的用于查找 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
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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码表现出未定义的行为。
在每个函数中,您将
min
和max
声明为局部变量。因此,当您在acceptNumbers()
中初始化min
/max
时,它们仅在该函数中初始化。您根本没有初始化High()
/Low()
中的min
/max
变量。当这些函数尝试取消引用那些未初始化的变量时,您的代码就会崩溃。此外,您的
High()
/Low()
函数具有非void
返回类型,但实际上并不是return
'ing任何值。试试这个:
Your code exhibits undefined behavior.
In each of your functions, you are declaring
min
andmax
as local variables. Thus, when you initializemin
/max
inacceptNumbers()
, they are only initialized in that function. You are not initializing themin
/max
variables inHigh()
/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 actuallyreturn
'ing any values.Try this instead:
函数 Low() 和 High() 仅声明 int* max、min 但从未初始化。尽管他们持有无效地址。
此函数会导致分段错误,因为您的程序尝试取消引用无效的内存地址。
如果您想存储
int *min, *max
,请将它们设置为全局或在 main() 中声明它们。The functions Low() and High() only declare the int* max, min but never initialize. Though they hold invalid addresses.
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().