【发布时间】:2016-01-07 19:55:40
【问题描述】:
我必须编写一个递归函数来搜索已排序的数组。
#include <iostream>
using namespace std;
int find(int value, int* folge, int max) {
}
int main() {
int const n = 7;
int wert1 = 4, wert2 = 13, wert3 = 2, wert4 = 25;
int folge[n] = {3,4,9,13,13,17,22};
wert1 = find(wert1, folge, n);
}
这是提供给我们的部分代码,我们必须完成它。
如果您有 4 个可用变量,我知道该怎么做。 (最小值和最大值)
但是我只有三个,有没有办法编辑给下一个函数的数组的起点?
【问题讨论】:
-
一个数组名在普通使用中衰减为一个指针。所以看起来你是通过名称传递一个数组,你实际上只是传递一个
int*。您可以通过简单地向其添加偏移量来递归地传递修改后的int*。find(value, folge+m, max-m) -
@SergeyA Wert 是我们要在数组中找到的数字
-
find在找到您想要的项目时应该返回什么?找不到时应该返回什么? -
非常好的问题,但我建议您在发布问题之前将变量名称翻译成英文,这样我们的工作会轻松很多。
标签: c++ arrays algorithm recursion binary-search