0

Why is my solution to binomial coefficient crashing? I've really tried to learn recursion, but I still think I am not clear about about it. I wonder if anyone can help me learn about recursion, and how to think recursively?

Even when I write a good base case, my program crashes. Any link to learn recursion with clear demonstration would be very helpful for me.

Here is my code for binomial coefficient, and I'm unable to find the bug/error, looking for your help.

code:

#include<stdio.h>   

long long m,n,o,p,result;  

long long binomial(long long  n,long long m)
{
   if(n==m)
       return 1;
   else {
       if(m==0)
           return 1;
       else {
           o=binomial(n-1,m);
           p=binomial(n-1,m-1);
           return o+p;
       }
   }
}

int main()
{
    printf("Please Enter The Value Of n:\n");
    scanf("%lld",&n);

    printf("Now Enter The value of m:\n");    
    scanf("%lld",&m);

    result = binomial(n,m);
    printf("Resultant Binomial coefficient: %lld\n",result);
    return 0;
}
Totem
  • 7,189
  • 5
  • 39
  • 66
Mobassir Hossen
  • 247
  • 4
  • 16
  • in this part: m=binomial(n-1,m); n=binomial(n-1,m-1); you can't replace your m value with the new result because you need the original m value for the 2nd binomial call. – algojava Feb 14 '17 at 14:12
  • @David Hoelzer thank you for your aid,but now i have made a change in my code,would you please see why it is still not working? here is the code: – Mobassir Hossen Feb 14 '17 at 14:17
  • #include long long m,n,result,o,p; long long binomial(long long n,long long m) { if(n==m)return 1; else{ if(m==0)return 1; else{ o=binomial(n-1,m); p=binomial(n-1,m-1); return o+p; } } } int main() { printf("Please Enter The Value Of n:\n"); scanf("%lld",&n); printf("Now Enter The value of m:\n"); scanf("%lld",&m); result = binomial(n,m); printf("Resultant Binomial coefficient: %lld\n",result); return 0; } – Mobassir Hossen Feb 14 '17 at 14:17
  • What values of n and m do you use? Note that since you test `n == m`, if you have `n < m` your first binomial `o=binomial(n-1,m);` will never exit but will constantly recurse. You should check that this does not happen. – sabbahillel Feb 14 '17 at 14:46
  • yes when n is less than m, say for example for 2 and 5 input my program crashes,i understand you that just because my base case checks for n==m then if n is less than m means my program will continue the recursive call untill the stack gets overflow,but mate i've tried this one,this link : http://www.ecst.csuchico.edu/~amk/foo/csci356/notes/ch1/solutions/recursionSol.html please check it.in that link the algorithm was same as i have written in my program? any solution to overcome from this trouble please? thank you – Mobassir Hossen Feb 14 '17 at 14:55
  • Clarified language used in question – Totem Feb 16 '17 at 17:41

1 Answers1

3

The binomial coefficient is only defined for pairs n and k when n >= k. It is conventional to use n and k in binomial coefficient expressions, but these correspond to n and m in your code.

You need to include some error-checking of input to avoid problems. Your code crashes when n is less than m because each time the statement o=binomial(n-1,m); is executed, the value of n in the called function is reduced, but n is already smaller than m, which is nonzero (if m were zero the function would have simply returned 1), so n == m can never occur.

Incidentally, you can improve your code in a few ways. Using global variables is generally a bad idea, and it would be better to move the declaration:

long long m, n, result;

into main(), where these variables are needed. Also, the function signature for main() should be int main(void). And you can tighten up the logic in the binomial() function considerably, removing the need for o and p:

long long binomial(long long  n,long long m)
{
    if (m != 0 && n != m) {
        return binomial(n-1, m) + binomial(n-1, m-1);
    } else {
        return 1;
    }
}
ad absurdum
  • 19,498
  • 5
  • 37
  • 60
  • i usually do not use global variables i know its a bad idea,thank you.hmm,That was really helpful mate and now i've understood my mistake,thank you a lot for your massive help @David Bowling – Mobassir Hossen Feb 14 '17 at 17:58