我一直试图弄明白这一点,似乎无法使其发挥作用。 为什么我的二进制搜索工作如果我手动输入数组长度但我交换手动输入:
int max = sizeof(list)/sizeof(list[0]);
它似乎不起作用。例如,如果我尝试在11,13,5,6,7,8,9,10的11元素数组中搜索2,则找不到2.但是如果我改变int min = 11.它可以找到2。 这是函数二进制排序:
int binarysearch(int *list, int sfor)
int min = 0;
int max = 10;
int mid = (min + max)/2;
while(min <= max)
{
if (sfor == list[mid])
{
printf("The number you are searching for is in %i place \n", mid + 1);
return 0;
}
else if (sfor < list[mid])
{
max = mid - 1;
}
else
{
min = mid + 1;
}
mid = (min + max)/2;
}
printf("Could not find number! \n");
return 1;
int main(int argc, string argv[]) // number searching for is passed through
{ // command line argument
printf("How many numbers did you want to search through? \n");
int a = GetInt(); //int a is how big should the array of numbers be
printf("What are they: \n");
int i = 0;
int b[a];
for (i = 0; i < a; i++)
{
b[i] = GetInt(); //cycles though each int of the array
}
int x = atoi(argv[1]); //converts command line argument to an int
binarysearch(b, x);
}
非常感谢任何帮助。
答案 0 :(得分:0)
你的问题在这里:
mAccess.getToken()
这对传递给函数的数组不起作用(因为你实际上无法将数组传递给函数)。您还必须传递数组的大小。