
Patrick B. answered 06/04/21
Math and computer tutor/teacher
/*******
Attempt #2 (AND FINAL !!!!).....
You CANNOT change the size of the array. Therefore, the index I was passing must then
be stored in the array at index ZER0. A[1] is the 1st fibonacci #, A[2] is the 2nd fibonacci #, etc;
*******************/
class Fibonacci
{
void Fibonacci_Array(int A[])
{
if (A!=null)
{
int N = A[0];
if (N>1)
{
A[0]--;
Fibonacci_Array(A);
A[N]=A[N-1]+A[N-2];
A[0]++;
}
else
{
A[1]=A[2]=1; //1st and 2nd Fibonacci Numbers are both ONE (1);
}
}
}
public static void main(String args[])
{
int MAX=20;
int A[] = new int[MAX+1];
A[0]=MAX;
Fibonacci fibonacci = new Fibonacci();
fibonacci.Fibonacci_Array(A);
int N=A[0];
for (int iLoop=1; iLoop<=N; iLoop++) { System.out.println(A[iLoop]); }
}
}
/*********************************************************************************
1st attempt
/**********************************************************************************/
class Fibonacci
{
void Fibonacci_Array ( int A[], int iIndexPos )
{
if (iIndexPos>0 && A!=null)
{
if (iIndexPos>1)
{
Fibonacci_Array(A,iIndexPos-1);
A[iIndexPos] = A[iIndexPos-1]+A[iIndexPos-2];
}
else
{
A[0]=A[1]=1;
}
} //valid array and index
}
public static void main(String args[])
{
int MAX=20;
int A[] = new int[MAX];
Fibonacci fibonacci = new Fibonacci();
fibonacci.Fibonacci_Array(A,MAX-1);
for (int iLoop=0; iLoop<MAX; iLoop++) { System.out.println(A[iLoop]); }
}
}
Jule J.
i cant add another parameter i only can have an array as a parameter06/04/21