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

发布于 2024-10-20 00:05:58 字数 1054 浏览 7 评论 0原文

我的问题是: 我有一个矩阵。我需要计算该矩阵中每个条目对应的斐波那契数,并将这些值返回到另一个矩阵中。 我不断收到 C2109“下标需要数组或指针类型”,我知道它来自哪里,我知道它的含义,但我不知道如何

  1. 修复它
  2. 以使我的代码正常工作。

现在,它没有任何作用。我不确定我是否从斐波那契函数返回任何值,或者在我的主函数中正确调用它。我已经根据原来的内容进行了修改。这是我的新代码:

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;
}

感谢任何帮助!

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

  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. 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心欲静而疯不止 2024-10-27 00:05:58

我认为问题在于这一行:

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

正在尝试错误地调用 fib 函数。您的想法是正确的,但是要在 C++ 中调用函数,您需要使用常规括号而不是方括号。我认为这一行应该看起来像

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

作为后续,您的 fib 实现似乎可能不正确。特别是,您接受了与矩阵相对应的两个参数,但您从不直接使用这些值。相反,您正在构建所有斐波那契数的新本地数组。您可能希望让此函数返回它生成的适当的斐波那契数。

希望这对您有所帮助,祝您 C++ 之旅好运!

I think the problem is that this line:

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

Is trying to call the fib function incorrectly. You have the right idea here, but to call a function in C++ you need to use regular parentheses rather than square brackets. 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. 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++!

雨落□心尘 2024-10-27 00:05:58

除了 templatetypedef 所说的之外,您只向 fib 函数提供一个参数,但声明它采用两个参数。此外 fib() 不返回值 - 它声明为 void。

fib( p [i][j] );

这里也少了一个分号

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

In addition to what templatetypedef said you are only supplying one argument to the fib function but declared it to take two arguments. Also the fib() doesn't return a value - its declared void.

fib( p [i][j] );

There is a semicolon missing also here

sum = fib[ m - 1]  +  fib[ m - 2]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文