用 C++ 计算斐波那契数代码
我的问题是: 我有一个矩阵。我需要计算该矩阵中每个条目对应的斐波那契数,并将这些值返回到另一个矩阵中。 我不断收到 C2109“下标需要数组或指针类型”,我知道它来自哪里,我知道它的含义,但我不知道如何
- 修复它
- 以使我的代码正常工作。
现在,它没有任何作用。我不确定我是否从斐波那契函数返回任何值,或者在我的主函数中正确调用它。我已经根据原来的内容进行了修改。这是我的新代码:
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
- fix it
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为问题在于这一行:
正在尝试错误地调用
fib
函数。您的想法是正确的,但是要在 C++ 中调用函数,您需要使用常规括号而不是方括号。我认为这一行应该看起来像作为后续,您的
fib
实现似乎可能不正确。特别是,您接受了与矩阵相对应的两个参数,但您从不直接使用这些值。相反,您正在构建所有斐波那契数的新本地数组。您可能希望让此函数返回它生成的适当的斐波那契数。希望这对您有所帮助,祝您 C++ 之旅好运!
I think the problem is that this line:
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 likeAs 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++!
除了 templatetypedef 所说的之外,您只向 fib 函数提供一个参数,但声明它采用两个参数。此外 fib() 不返回值 - 它声明为 void。
这里也少了一个分号
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.
There is a semicolon missing also here