Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;函数需要大小\u t参数 我设计了一个C++二进制搜索方法,它包含10个整数数组和一个整数来搜索。我设计了main方法,从命令行参数接收数组,并提示用户搜索整数。两者的地址都传递给bsearch方法(因为直接传递它们似乎不起作用),然后该方法遍历数组并搜索提供的目标。我的b搜索方法的代码发布在下面: void bsearch(int array[10], int key) { int candidate; int min = 0; int max = sizeof(array)/sizeof(array[0]); bool found = false; while(!found) { //Begin iterative loop if(max<min) break; //cout << key << " not found" << endl; //Only executes after searching entire array for(int i=min;i<max;i++) { cout << array[i] << " "; //Prints out current } //section being searched cout << endl; candidate = array[(max+min)/2]; //Check middle element if(candidate == key) { found = true; //Target located } else if(candidate>key) { max = ((max+min)/2)-1; //Search lower portion } else if(candidate<key) { min = ((max+min)/2)+1; //Search upper portion } } if(found) cout << key << " found at index " << (max+min)/2 << endl; //Report target location else cout << key << " not found" << endl; //Report target not found } void b搜索(int数组[10],int键){ int候选人; int min=0; int max=sizeof(数组)/sizeof(数组[0]); bool-found=false; 而(!found){//开始迭代循环 如果(max_C++_Arrays_Parameters_Binary Search_Size T - Fatal编程技术网

C++;函数需要大小\u t参数 我设计了一个C++二进制搜索方法,它包含10个整数数组和一个整数来搜索。我设计了main方法,从命令行参数接收数组,并提示用户搜索整数。两者的地址都传递给bsearch方法(因为直接传递它们似乎不起作用),然后该方法遍历数组并搜索提供的目标。我的b搜索方法的代码发布在下面: void bsearch(int array[10], int key) { int candidate; int min = 0; int max = sizeof(array)/sizeof(array[0]); bool found = false; while(!found) { //Begin iterative loop if(max<min) break; //cout << key << " not found" << endl; //Only executes after searching entire array for(int i=min;i<max;i++) { cout << array[i] << " "; //Prints out current } //section being searched cout << endl; candidate = array[(max+min)/2]; //Check middle element if(candidate == key) { found = true; //Target located } else if(candidate>key) { max = ((max+min)/2)-1; //Search lower portion } else if(candidate<key) { min = ((max+min)/2)+1; //Search upper portion } } if(found) cout << key << " found at index " << (max+min)/2 << endl; //Report target location else cout << key << " not found" << endl; //Report target not found } void b搜索(int数组[10],int键){ int候选人; int min=0; int max=sizeof(数组)/sizeof(数组[0]); bool-found=false; 而(!found){//开始迭代循环 如果(max

C++;函数需要大小\u t参数 我设计了一个C++二进制搜索方法,它包含10个整数数组和一个整数来搜索。我设计了main方法,从命令行参数接收数组,并提示用户搜索整数。两者的地址都传递给bsearch方法(因为直接传递它们似乎不起作用),然后该方法遍历数组并搜索提供的目标。我的b搜索方法的代码发布在下面: void bsearch(int array[10], int key) { int candidate; int min = 0; int max = sizeof(array)/sizeof(array[0]); bool found = false; while(!found) { //Begin iterative loop if(max<min) break; //cout << key << " not found" << endl; //Only executes after searching entire array for(int i=min;i<max;i++) { cout << array[i] << " "; //Prints out current } //section being searched cout << endl; candidate = array[(max+min)/2]; //Check middle element if(candidate == key) { found = true; //Target located } else if(candidate>key) { max = ((max+min)/2)-1; //Search lower portion } else if(candidate<key) { min = ((max+min)/2)+1; //Search upper portion } } if(found) cout << key << " found at index " << (max+min)/2 << endl; //Report target location else cout << key << " not found" << endl; //Report target not found } void b搜索(int数组[10],int键){ int候选人; int min=0; int max=sizeof(数组)/sizeof(数组[0]); bool-found=false; 而(!found){//开始迭代循环 如果(max,c++,arrays,parameters,binary-search,size-t,C++,Arrays,Parameters,Binary Search,Size T,bsearch(&searchArray,&target)与您的bsearch(int[],int key)的签名不匹配。但是,前两个参数确实与的签名匹配,您可能无意中通过以下方式将其引入命名空间: #include <cstdlib> using namespace std; #包括 使用名称空间std; b搜索(&searchArray,&target)与您的b搜索(int[],int key)的签名不匹配。但是,前两个参数确实与的签名匹配,您可能无意中通过以下方式将其引入命名

bsearch(&searchArray,&target)
与您的
bsearch(int[],int key)
的签名不匹配。但是,前两个参数确实与的签名匹配,您可能无意中通过以下方式将其引入命名空间:

#include <cstdlib>
using namespace std;
#包括
使用名称空间std;
b搜索(&searchArray,&target)
与您的
b搜索(int[],int key)
的签名不匹配。但是,前两个参数确实与的签名匹配,您可能无意中通过以下方式将其引入命名空间:

#include <cstdlib>
using namespace std;
#包括
使用名称空间std;

标准库中有一个函数。您传递的参数不正确。
int max=sizeof(array)/sizeof(array[0]);
这是错误的。请尝试打印它。标准库中有一个函数。您传递的参数不正确。
int max=sizeof(array)/sizeof(array[0]);
这是错误的。请尝试打印它。这很令人惊讶,我没想到会有一个同名的库函数。非常感谢!而且,我意识到我传入参数的方式是错误的,但实际上我这样做是为了安抚编译器。我只需重命名该方法并更改回参数,然后。这是一个(又一个)很好的例子说明了为什么在代码中使用命名空间std;总是一件坏事。即使你的程序有3行。这很令人惊讶,我没想到会有一个同名的库函数。非常感谢!另外,我意识到我传入参数的方式是错误的,但实际上我是按顺序这样做的为了安抚编译器,我只需要重命名该方法并更改回参数。这是一个(又一个)很好的例子,说明了为什么在代码中使用命名空间std;总是一件坏事。即使程序只有3行。