二进制搜索程序无法识别边界?
Binary search program won't recognize bounds?
我正在编写一个简单的二进制搜索程序,其中包含 10 个元素。 这是我的代码;
int main(int argc, char *argv[]) {
int query, pos;
int A[10] = {0,1,2,3,4,5,6,7,8,9}; //contents of the array
cout<<"enter query = ";
cin>>query;
int x=(10-1)/2; //the middle ground
bool found = false;
if(query<A[x]){ //if query is less than A[x] search before i
for(int i=0;i<=x;i++){
if(A[i]==query){
found=true;
pos=i;
}
}
}
if(query>A[x]){
for(int i=4;i<10;i++){
if(A[i]==query){
found=true;
pos=i;
}
}
}
if (found=false){
cout<<"NOT FOUND"<<endl;
}
else{
cout<<"FOUND AT A["<<pos<<"]"<<endl;
}
return 0;
}
奇怪的是,如果我尝试搜索一个不存在的元素,它会返回 FOUND AT A[48092] 或其他东西。我哪里做错了?
您看到的数字是变量 pos 的值,您看到它的原因是因为拼写错误:
if (found=false)
应该是
if (found == false)
相关文章:
- 提升 ASIO 无法识别计时器对象
- 从udp接收帧对于人脸识别来说太慢
- 模板类无法识别友元运算符
- std::当在256字节边界上写入整数时,流的奇怪行为
- std::visit无法识别类型
- 无法识别模板功能
- 我有两个类需要在同一 cpp 文件中相互引用,但第一个类无法识别第二个类类型的对象
- 如何在LLVM中dyn_cast以识别StoreInst?
- 使用不变量来确定二分搜索中的边界条件
- 从不同的附加依赖项中识别等同命名的函数
- 跨 DLL 边界访问虚拟方法是否安全/可能?
- C++出现控制台错误.我无法识别源代码的问题
- VSCode IntelliSense无法识别SDL框架的SDL_image扩展库
- Qt:"Q3DScatter"即使包含在内也无法识别
- 在 64 位边界上对齐C++结构数组?
- 使用指针的类识别
- 如何静态识别动态堆分配?
- OpenGL 泛洪填充无法识别边界
- 删除边界框内的边界框以进行数字识别
- 二进制搜索程序无法识别边界?