Latest update on December 7, 2010 at 05:16 AM by aakai1056 .
Calculate the exponential by recursion
Issue
Please, I would like to know the error (s) that exists in this algorithm. For information: this algorithm is to compute the exponential method by recursion.
#include
", x, s); }
Solution
Several errors.
I pass over the absurdity of using recursion to calculate the factorial and power, I suppose you requested.
First, detailed syntax ultra-classic: the test is ==, not =. In your two functions should be if (i == n +1)
Worse, in the else test both functions, there is no return. So your function returns nothing, it may take a random value. I guess the idea was to have the first return factorial (n, i, fact);
But even with this correction factor and Thy power will not work. You make fine calculations, then returned to the stage n +1, you can force the result to 1!
Finally, in your hand, you have reinitialize p and fact on each loop.
In my opinion it is better to write the factorial function and power to work by decreasing values (from n to 1) rather than increasing from 1 to n +1. Function only requires one parameter, it is much simpler (no i, Fact, or p)
Note
Solve by le père
Leave a Comment