Fibonacci sequence

#include <iostream>
int array(int);
using namespace std;
int main()
{

cout<<array(100)<<endl;//print the result
system("pause");
return 0;



}
int array(int)
{
int i;
if (i==1)
return 0;
else
array(i)= array(i-1)+array(i-2);//construction of the fibonacci sequence//

return array(i);

}
//I don't know why it is not working.
You've declared i locally, you need to make it an argument in the function definition.
1
2
3
4
5
6
7
8
9
10
11
int array(int)
{
int i;
if (i==1)
return 0;
else
array(i)= array(i-1)+array(i-2);//construction of the fibonacci sequence//

return array(i);

}


Change your function to:
1
2
3
4
5
6
7
8
9
10
int array(int i)
{
if (i==1)
return 0;
else
array(i)= array(i-1)+array(i-2);//construction of the fibonacci sequence//

return array(i);

}
thanks but it still has problem//
1>------ Build started: Project: fab arry, Configuration: Debug Win32 ------
1> fab.cpp
1>c:\users\danny\documents\visual studio 2010\projects\fab arry\fab arry\fab.cpp(16): error C2082: redefinition of formal parameter 'i'
1>c:\users\danny\documents\visual studio 2010\projects\fab arry\fab arry\fab.cpp(20): error C2106: '=' : left operand must be l-value
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
In this case, you've put i as a formal parameter, but you haven't deleted the local definition. Delete the line in the function that shows int i;.
Topic archived. No new replies allowed.