Finding Lowest Value. Function. Error: Thread 1: EXC_BAD_ACCESS (Code =1, address = 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);
我正在开发一个函数,用于在 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);