Reputation: 3
Please tell me what is wrong, the output is wrong, shows 4 to me F1 = 2, Fn = (Fn-1) 2 , n ≥ 2 i. What is the complexity of the algorithm that computes Fn using the recursive definition given above.
#include <iostream>
using namespace std;
double calcFn(double,int);
int main()
{
cout << calcFn(2, 4);
cout << endl;
cin.get();
return 0;
}
double calcFn(double F1, int N)
{
cout << endl;
double Fn = F1 * F1;
while (N > 0)
{
N--;
calcFn(Fn, N);
}
return Fn;
}
// CODE DOESNT SHOW the RIGHT oUTPUT
Upvotes: 0
Views: 68
Reputation: 112
First of all, You have recurrency, so you should not use the loop. Secondly, You do not assign return value. The corrected snippet:
if (N > 0)
{
N--;
Fn = calcFn(Fn, N);
}
Still, I'm not sure about the result, but You can further correct it.
Upvotes: 1