简体   繁体   English

用C ++代码计算斐波那契数

[英]Calculating Fibonacci Numbers in C++ code

My question is: I have a matrix. 我的问题是:我有一个矩阵。 I need to calculate the corresponding Fibonacci number to each entry in that matrix, and return those values into another matrix. 我需要为该矩阵中的每个条目计算相应的斐波那契数,然后将这些值返回到另一个矩阵中。 I keep getting a C2109 "Subscript requires array or pointer type", and I know where it's coming from, and I know what it means, but I don't know how to 我不断收到C2109“下标需要数组或指针类型”,我知道它的来源,我知道它的意思,但是我不知道该怎么做。

  1. fix it 修理它
  2. make my code work. 使我的代码工作。

Right now, it doesn't do anything. 现在,它什么也没做。 I'm not sure if I'm even returning any value from my Fibonacci function, or calling it correctly in my main function. 我不确定我是从Fibonacci函数返回任何值,还是在主函数中正确调用它。 I've modified it from what it originally was. 我已经修改了它原来的样子。 Here's my new code: 这是我的新代码:

const int     row1 = 3;
const int     col1row2 = 3;
const int     col2 = 3;

int fibonacci (int [][col2]);

void main()
{
     int   p[row1][col2],  f [row1][col2];
     int sum; 
     input (a,b); 

     cout<<"The Fibonacci Matrix is:   ";
     cout<<fibonacci(p);    
     for ( int  i = 0; i < row1; i++)
     {
          for ( int  j = 0; j < col2; j++)
              {
                    sum = f[i][j]; 
                    f[i][j] = fibonacci(p);           
              }
     }
     cout<<endl;
}


int fibonacci (int z[][col2])
{
     int   fib [100]  =  {0 , 1};
     int sum = 0;

     for ( int m = 2; m < 100; m++)
     {
           sum =  fib[m-1] + fib[m-2];
           fib[m] = sum;
     }
     return sum;
     cout<<endl;
}

Any help is appreciated! 任何帮助表示赞赏!

I think the problem is that this line: 我认为问题是这一行:

f [i][j] = fib [ p [i][j] ];

Is trying to call the fib function incorrectly. 试图错误地调用fib函数。 You have the right idea here, but to call a function in C++ you need to use regular parentheses rather than square brackets. 您在这里有一个正确的想法,但是要在C ++中调用函数,您需要使用常规括号而不是方括号。 I think this line should look like 我认为这条线应该看起来像

f [i][j] = fib ( p [i][j] );

As a follow-up, your implementation of fib seems like it might be incorrect. 作为后续,您对fib的实现似乎不正确。 In particular, you're taking in two parameters corresponding to the matrices, but you never use those values directly. 特别是,您要输入与矩阵相对应的两个参数,但您永远不要直接使用这些值。 Instead, you're constructing a new local array of all the Fibonacci numbers. 相反,您正在构建一个包含所有斐波那契数字的新本地数组。 You will probably want to have this function return the appropriate Fibonacci number it generates. 您可能希望此函数返回它生成的适当的斐波那契数。

Hope this helps, and best of luck on your journey into C++! 希望这会有所帮助,并祝您进入C ++旅途顺利!

In addition to what templatetypedef said you are only supplying one argument to the fib function but declared it to take two arguments. 除了templatetypedef所说的以外,您只向fib函数提供了一个参数,但声明它接受了两个参数。 Also the fib() doesn't return a value - its declared void. 同样,fib()不返回值-声明为void。

fib( p [i][j] );

There is a semicolon missing also here 这里也缺少分号

sum = fib[ m - 1]  +  fib[ m - 2]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM