
这个c语言程序的binary search貌似只有一个错了,但是不知道怎么改,求大神帮忙 10
/***helpers.c**HelperfunctionsforProblemSet3.*/#include<cs50.h>#include<stdio.h>#incl...
/**
* helpers.c
*
* Helper functions for Problem Set 3.
*/
#include <cs50.h>
#include <stdio.h>
#include "helpers.h"
/**
* Returns true if value is in array of n values, else false.
*/
bool search(int value, int values[], int n)
{ int c, first, last, middle, search;
printf("Enter number of elements\n");
value = get_int();
printf("Enter %d integers\n", n);
values[];
for (c = 0; c < n; c++)
printf("Enter value to find\n");
search = get_int();
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if ( values[middle] < value)
first = middle + 1;
else if ( values[middle] == value) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", value);
return 0;
}
/**
* helpers.c
*
* Helper functions for Problem Set 3.
*/
#include <cs50.h>
#include <stdio.h>
#include "helpers.h"
/**
* Returns true if value is in array of n values, else false.
*/
bool search(int value,int values[], int n)
{ int c, first, last, middle;
printf("Enter number of elements\n");
n = get_int();
printf("Enter %d integers\n", n);
values[] = get_int;
for (c = 0; c < n; c++)
printf("Enter value to find\n");
value = get_int();
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if ( values[middle] < value)
first = middle + 1;
else if ( values[middle] == value) {
printf("%d found at location %d.\n", value, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", value);
return 0;
}
求改这个啊,原来那个不对 展开
* helpers.c
*
* Helper functions for Problem Set 3.
*/
#include <cs50.h>
#include <stdio.h>
#include "helpers.h"
/**
* Returns true if value is in array of n values, else false.
*/
bool search(int value, int values[], int n)
{ int c, first, last, middle, search;
printf("Enter number of elements\n");
value = get_int();
printf("Enter %d integers\n", n);
values[];
for (c = 0; c < n; c++)
printf("Enter value to find\n");
search = get_int();
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if ( values[middle] < value)
first = middle + 1;
else if ( values[middle] == value) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", value);
return 0;
}
/**
* helpers.c
*
* Helper functions for Problem Set 3.
*/
#include <cs50.h>
#include <stdio.h>
#include "helpers.h"
/**
* Returns true if value is in array of n values, else false.
*/
bool search(int value,int values[], int n)
{ int c, first, last, middle;
printf("Enter number of elements\n");
n = get_int();
printf("Enter %d integers\n", n);
values[] = get_int;
for (c = 0; c < n; c++)
printf("Enter value to find\n");
value = get_int();
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if ( values[middle] < value)
first = middle + 1;
else if ( values[middle] == value) {
printf("%d found at location %d.\n", value, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", value);
return 0;
}
求改这个啊,原来那个不对 展开
2个回答
展开全部
虽然这个程序自定义了输入函数,但是二分查找的逻辑是没有错的,不知道你说的错误是什么。
2018-07-29
展开全部
第一行bool形放后头去,#include<......>后头
还有 第一行的#include<>要另起一行
还有 第一行的#include<>要另起一行
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询