Here's what I thought of so far which works for 2 elements in the array. the elements in the array are the variables to be plugged in to the continued fraction.
double continuedFraction(int a[], int size)
{
double fraction = a[0];
for(int i = 1; i < size; i++)
{
fraction += (double)1/(double)a[i];
}
return fraction;
}
btw I'm not using recursion I need to be able to get the continued fraction result.
For the first iteration, you get (a0 + 1/a1). In further iterations, your code keeps adding the inverse of given number which is (a0 + 1/a1) + (1/a2) + (1/a3) + ...
What you ideally need is an + ... 1/(a2 + 1/(a1 + 1/a0))...)
double continuedFraction(int a[], int size)
{
double fraction = a[0];
for(int i = 1; i < size; i++)
{
fraction = a[i] + (1/fraction);
}
return fraction;
}
If you want the other way around, a0 + 1/(a1 + 1/(a2 + 1/a3 + ... ))...) you could run the loop from array_size-1 to 0