Thread: Recursive function

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    5

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

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    5
    By the way some guy called Napier came up with the algorithm, I know the maths is kind of confusing but it does work

  3. #3
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    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;
           */
    
    }
    Last edited by Happy_Reaper; 10-25-2006 at 05:10 PM.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    5
    Nice one, thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM