Thread: recursion to find binomial

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    18

    recursion to find binomial

    recursion to find binomial-untitled-pnghi can someone help me check if my recursion is right? i cant seems to get the answer
    Code:
    #include<stdio.h>
    int getBinomial(int, int);
    
    
    int main(void)
    {
    	int n,k;
    	printf("enter :");
    	scanf("%d %d", &n, &k);
    	printf("binomial =%d", getBinomial(n,k));
    
    
    	return 0;
    }
    
    
    int getBinomial(int n, int k)
    {
    	while(n>=0)
    	{
    		if(k==0 || k==n)
    		return 1;
    	}
    	
    	if(n>k && k>0)
    	return getBinomial(n-1,k) + getBinomial(n-1, k-1);
    
    
    }
    Last edited by wonderwall; 10-22-2011 at 04:02 AM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    For starts you really do need to learn how to indent code to visually represent what's going on. Both indentation and vertical whitespace make programs far more readable.

    Second C is probably the worst name for a function I've ever seen... Give your variables and functions meaningful names... GetBinomial()... etc.

    Finally... throw in some diagnostic printf() statements so you can see what's going on... they'll tell you the story.

    And, no, I'm not going to compile your code and test it for you...

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
        while(n>=0)
        {
            if(k==0 || k==n)
            return 1;
        }
    What good can that loop possibly be?


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    18
    Thanks Quzah! i think i just created a infinite loop!
    Code:
    #include<stdio.h>
    int getBinomial(int, int);
    
    
    int main(void)
    {
    	int n,k;
    	printf("enter :");
    	scanf("%d %d", &n, &k);
    	printf("binomial =%d", getBinomial(n,k));
    
    
    	return 0;
    }
    
    
    int getBinomial(int n, int k)
    {
    	if(k==0 || k==n)
    		return 1;
    	
    	
    	if(n>k && k>0)
    		return getBinomial(n-1,k) + getBinomial(n-1, k-1);
    	
    }
    It can run now! THANKS!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binomial Simulation
    By quintenmater in forum C Programming
    Replies: 12
    Last Post: 12-04-2010, 08:33 PM
  2. Segmentation fault using recursion to find factorial
    By kapok in forum C++ Programming
    Replies: 4
    Last Post: 02-23-2009, 11:10 AM
  3. how to find a mathematical formula for this recursion??
    By transgalactic2 in forum C Programming
    Replies: 12
    Last Post: 10-12-2008, 02:23 PM
  4. A binomial queue (binomial tree) question.
    By NightWalker in forum C Programming
    Replies: 2
    Last Post: 10-27-2003, 08:25 AM
  5. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 09:28 AM