C 程序崩溃(数组和2个指针Binsearch功能)
C++ program crashing (array and 2 pointer binsearch function)
当我运行此程序并输入一个值以搜索程序崩溃时(exe
已停止工作,关闭程序)。一次输入值65时,我找到一个未找到的无限循环,输入一个值进行搜索(-1退出):
这是代码:
#include <iostream>
using namespace std;
void Search(int[], int * , int * );
int main()
{
int i,KEY,num, array[] = {98,87,76,65,54};
for(i=0;i<5;i++)
cout << array[i] << " ";
Search(array, &KEY, &num);
cout << endl;
system("pause");
return 0;
}
void Search(int arr[5], int * current, int * numel)
{
int low, high,search,N;
cout << "nWhat Number would you like to search for? (-1 to quit) : ";
cin >> search;
while(search!=-1)
{
low=0;
high=N-1;
while(low<=high)
{
*current=(low+high)/2;
if (search > arr[*current])
low=*current+1;
else if(search<arr[*current])
high=*current-1;
else
break;
}
if(arr[*current]==search)
cout << "Number found at index " << *current << endl;
else
cout << "Number not found." << endl;
cout << "Enter a value to search (-1 to quit) :";
}
return;
}
对于初学者来说,如果不在数组中的数字中,则Search
中的主循环是没有办法的。
然后,您正在使用high
之前使用任何值。
可能还有其他问题。您是如何在开发时测试的?
N
未初始化,所以谁知道这个说法:
high=N-1;
会做?
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 为什么使用 "this" 指针调用派生成员函数?
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用指针从C++中的数组中获取最大值
- 助记符和指向成员语法的指针
- 嵌入方指针压缩已禁用
- 数组的指针从不分段故障
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 何时在引用或唯一指针上使用移动语义
- QMetaObject invokeMethod的基于函数指针的语法
- 如何从 std::atomic 中提取指针 T<T>?
- 如何在 C# 中映射双 C 结构指针?
- C++将浮点指针值舍入为小数位数
- 为什么++(*p)更改指针值
- 调整大小后指向元素值的指针unordered_map有效?
- 正在将指针转换为范围
- 使用指向成员的指针将成员函数作为参数传递
- 将OpenCV C++重写为EmguCV C#-如何使用指针
- C 程序崩溃(数组和2个指针Binsearch功能)