keerat
keerat

Reputation: 3

2 to the power 2 n times incorrect

Please tell me what is wrong, the output is wrong, shows 4 to me F​1​ = 2, F​n​ = (F​n-1​)​ 2​ , n ≥ 2 i. What is the complexity of the algorithm that computes ​F​n 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

Answers (1)

b00rt00s
b00rt00s

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

Related Questions