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; }



LinkBack URL
About LinkBacks


