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
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!
In the function, we first check if the number n is zero or one. If yes, we return the value of n. If not, we recursively call fibonacci with the values n-1 and n-2. These are the ways of generating a Fibonacci series in C.
The Fibonacci numbers are the numbers in the following integer sequence. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …….. F0 = 0 and F1 = 1.
Fibonacci Series Used to Teach Recursive Programming.
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++!
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With