Thread: Problem with recurrsion

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    13

    Question Problem with recurrsion

    Hi, All

    I'm trying to call a function recursively. I tried this code and it doesnt seem to work correctly. The code compiles perfectly but it gives the wrong output. Can you guys suggest what might be wrong in here..

    Code:
    #include<stdio.h>
    #include<math.h>
    int rec(int[],int,int);
    
    void main()
    {	
    
    	int i,a[18],ans;
    	//int a[8]={50,20,40,20,10,50,20,50};
    	printf("Enter the elements of array");
    	for(i=0;i<8;i++)
    	scanf("%d",&a[i]);
    	ans=rec(a,1,8);
            printf("%d",ans);
    }
    
    int rec(int a[],int i,int j)
    	{
    		int m,y,z;
    		if(i>j)
    //		printf("Error");
                    return 0;
    		else if (i==j)
    		//printf("%d",a[i]);
                    return a[i];
    		else
    		{
    			m=floor((i+j)/2);
    			y=rec(a,i,m);
    			z=rec(a,m+1,j);
    			return y+2*z;
    		}
    	}
    Last edited by newbie123; 09-01-2010 at 05:33 PM.

  2. #2
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    It may not be relavent, but what does (or should) rec( ) return if i>j or i==j?

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    13
    if i>j then it shud return an error.. if it is equal to j, it shud return a[i]

  4. #4
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Those are what you are sending to the output. The question is: What should the return value from the rec( ) fuction when you are outputing those values?

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    13
    I had changed the return statements in the function.. Let me knw if it makes sense now?

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    13
    I figured out where I went wrong.. Thanks for your help.. It is what made me think

  7. #7
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Well now it does not have an obvoius mistake. I do not understand what it is supposed to calculate. I do not understand what the result produced means.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM