C++ 固定递归数组

C++ 固定递归数组,c++,arrays,recursion,C++,Arrays,Recursion,可能重复: 我必须修改一些代码来做一些新的事情 这些是新的要求: #include <iostream> using namespace std; // Returns the nth number in the fibonacci sequence int fib(int n, int* memory); const int MAXFIB = 46; int main() { int memory[MAXFIB]; memory[0] = 1; m

可能重复:

我必须修改一些代码来做一些新的事情

这些是新的要求:

#include <iostream>
using namespace std;

// Returns the nth number in the fibonacci sequence
int fib(int n, int* memory);

const int MAXFIB = 46;

int main()
{
    int memory[MAXFIB];

    memory[0] = 1;
    memory[1] = 1;

    cout << fib(46) << endl;

    system("pause");
    return 0;
}

int fib(int n, int* memory)
{
    // Base cases
    if (n < 1 || n > MAXFIB) return -1;

    if (n == memory) return memory;

    // Recursive cases
    memory[n] = fib(n-1) + fib(n-2);
    return;

}   
-该程序将需要使用一个数组来将计算出的斐波那契数存储在内存中。(可以是全局变量)

-numbers数组也被认为是一个部分填充的数组,因此我需要声明一个变量来跟踪数组中存储的项数

-数字数组应声明为有46个位置

-数字46应该声明为名为MAXFIB的全局常量,因此数字46不应该在我的程序中出现多次

-在程序开始时,需要使用“起始知识”(基本情况)初始化数字数组-前两个斐波那契数是1和1。将这些存储到前两个数组位置

-添加一组额外的基本情况,以便如果“n”参数值太低或太高,则函数将停止并返回-1,指示错误情况

-添加一个额外的基本情况,这样,如果我们正在寻找的斐波那契数已经存储在数组中,那么我只需要从数组中检索它并返回它

-对于递归情况,它仍然需要像以前一样递归调用函数两次,但是在计算完数字之后,我需要在返回之前将其存储到数组中

这是我目前的代码:

#include <iostream>
using namespace std;

// Returns the nth number in the fibonacci sequence
int fib(int n, int* memory);

const int MAXFIB = 46;

int main()
{
    int memory[MAXFIB];

    memory[0] = 1;
    memory[1] = 1;

    cout << fib(46) << endl;

    system("pause");
    return 0;
}

int fib(int n, int* memory)
{
    // Base cases
    if (n < 1 || n > MAXFIB) return -1;

    if (n == memory) return memory;

    // Recursive cases
    memory[n] = fib(n-1) + fib(n-2);
    return;

}   
#包括
使用名称空间std;
//返回斐波那契序列中的第n个数字
int fib(int n,int*内存);
常数int MAXFIB=46;
int main()
{
int内存[MAXFIB];
内存[0]=1;
存储器[1]=1;

cout不太精通特定的语言,但在对fib的方法调用中,您只输入了46。在fib()的定义中,您指定了两个参数。可能在方法调用中,添加参数以填充int*内存

再说一次。不精通这门语言。可能吧。

你为什么要用46
你有没有特殊的要求?作业?你试过编译你的代码吗?错误信息告诉你什么关于你的代码?至少在尝试之前编译你的代码。而C++标签绝对是你必须精通语言来回答问题的答案,这个答案是正确的,因为它回答你的意思。“要填充int*内存”?int fib(int n,int*内存){这是您定义函数的方式。.当您从main()调用此函数时,只需将其称为fib(46),您需要fib(46,someInt)