# Recursive function

• 10-25-2006
Fork
Recursive function
This program I wrote calculates the log of a certain number. Problem is we're supposed to use a recursive function, I tried to use one but the way I have it it could easily be replaced by putting the while loop around the whole function and not bothering at all with making it recursive. Any ideas on a better recursive function here? I'm new enough to programming, any help is much appreciated. Anyway here's the code

Code:

``` #include <stdio.h> #include <math.h> float logarithm(float m, float n, float x, float y, float c); int main(void) {         float m,n,x,y,c,ans;         printf("******** WELCOME TO LOGGER ********");         printf("\n\nEnter the lower value of range: ");         scanf("%f", &m);                 printf("Enter the corresponding log (base 10): ");         scanf("%f", &x);         printf("\nEnter the highest value in range: ");         scanf("%f", &n);         printf("Enter the corresponding log (base 10): ");         scanf("%f", &y);                 printf("\nEnter the value whose logarithm you wish to find: ");         scanf("%f", &c);         ans=logarithm(m, n, x, y, c);         printf("\nThe estimated logarithm is %f", ans);         } float logarithm(float m, float n, float x, float y, float c) {         float ans,g,a;                 g=sqrt(m*n);         a=(x+y)/2;                 if (g<c)         {                 m=g;                 x=a;         }         else if (g>c)                 {                 n=g;                 y=a;                }         while ((fabs(m-n))>0.01)         {                 return (logarithm(m, n, x, y, c));         }         return a; }```
• 10-25-2006
Fork
By the way some guy called Napier came up with the algorithm, I know the maths is kind of confusing but it does work
• 10-25-2006
Happy_Reaper
Code:

```float logarithm(float m, float n, float x, float y, float c) {         float ans,g,a;                 g=sqrt(m*n);         a=(x+y)/2;                 if (g<c)         {                 m=g;                 x=a;         }         else if (g>c)                 {                 n=g;                 y=a;                }         while ((fabs(m-n))>0.01)         {                 return (logarithm(m, n, x, y, c));         }         return a; }```
This is sort of recursive, but you're making it more complicated then it has to be, and thus losing much of the elegance of the recursive solution :

Code:

```float logarithm(float m, float n, float x, float y, float c) {         float ans,g,a;         /*  Add this part to eliminate the while loop */         if((fabs(m-n))>0.01)         {                 return a;         }         g=sqrt(m*n);         a=(x+y)/2;                 if (g<c)         {                 /*  Don't do this                 m=g;                 x=a;                 */                 /*  Use this instead  */                 logarithm(g,n,a,y,c);         }         else if (g>c)                 {                 /*  Same as above                 n=g;                 y=a;                        */                 logarithm(m,g,x,a,c);         }         /*    Now this loop is useless         while ((fabs(m-n))>0.01)         {                 return (logarithm(m, n, x, y, c));         }         return a;       */ }```
• 10-26-2006
Fork
Nice one, thanks for the help :D