查找最低值.功能.错误:线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x7fff5fc89000)
Finding Lowest Value. Function. Error: Thread 1: EXC_BAD_ACCESS (Code =1, address = 0x7fff5fc89000)
我正在研究一个函数,以找到 5 个输入双精度值中的最小值。我在网上找到了一个参考资料。我尝试使用数组执行代码,但是收到以下错误:错误:线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x7fff5fc89000)
你能告诉我我的代码哪里有问题吗?欣赏它!
在线参考:
int findLowest(int s1, int s2, int s3, int s4, int s5)
{
int lowest = s1;
if (s2 < lowest )
{
lowest = s2;
}
else if (s3 < lowest)
{
lowest = s3;
}
else if (s4 < lowest)
{
lowest = s4;
}
else if (s5 < lowest)
{
lowest = s5;
}
cout << "The lowest test score is: " << lowest << endl;
return lowest;
}
来源: http://cboard.cprogramming.com/cplusplus-programming/149549-lowest-score-drop-assignment.html
我的数组代码:
double findLowest(double a, double b, double c, double e, double f)
{
// creating an array numberRange[] to read the 5 input double
//values
double numberRange[5] = {a,b,c,e,f};
// creating a variable minimum and assigning it the value of the
//first item in the numberRange[] array
double minimum = numberRange[0];
// looping through the numberRange array
for(int counter = 1; sizeof(numberRange)/sizeof(*numberRange) ; counter++ )
// To get the number of elements in an array, you have to divide
// the size of the array by the size of each element
{
// checking if the numberRange value at the counter is less
// than the minimum value. If true, the minimum value is
//replaced with a new value.
if ( numberRange[counter] < minimum )
{
minimum = numberRange[counter];
}
}
return minimum;
}
错误:线程 1:EXC_BAD_ACCESS(代码 =1,地址 = 0x7fff5fc89000)
问题是你的 for 循环测试:
for(int counter = 1; sizeof(numberRange)/sizeof(*numberRange) ; counter++ )
// this ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ is always true!
为什么不把它counter < 5
?
你的 for 循环的停止条件非常不寻常。你也从 1 开始计数(为什么不是 0)?只是做一个怎么样:
double numberRange[5] = {a,b,c,e,f};
return *std::min_element(numberRange);
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 代码在main()中运行,但在函数中出现错误
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 编译包含字符串的代码时遇到问题
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 在Linux for Windows上编译C++代码时出错
- 我的字符计数代码计算错误.为什么
- 孤立代码块在结构中引发异常
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 此代码是否违反一个定义规则
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 为什么在这个代码结束循环中没有得到结束
- 在c代码之间共享数据的最佳方式
- 查找最低值.功能.错误:线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x7fff5fc89000)