Segmentation fault in C recursive Combination (nCr)

Posted by AruniRC on Stack Overflow See other posts from Stack Overflow or by AruniRC
Published on 2010-04-17T16:22:49Z Indexed on 2010/04/17 16:33 UTC
Read the original article Hit count: 251

Filed under:
|
|

PLease help me out here. The program is supposed to recursively find out the combination of two numbers. nCr = n!/ (r!(n-r)! ). I'm getting this error message when i compile it on GCC.

Here's what the terminal shows:

Enter two numbers: 8 4 Segmentation fault


(Program exited with code:139)

The code is given here:

    #include<stdio.h>

float nCr(float, float, float);

int main()
{

 float a, b, c;
 printf("Enter two numbers: \n");
 scanf("%f%f", &a, &b);
 c = nCr(a, b, a-b);
 printf("\n%.3f", c);
 return 0;
}

float nCr(float n, float r, float p)
{

        if(n<1)
 return (1/(p*r))*(nCr(1, r-1, p-1));

 if(r<1)
 return (n/(p*1))*(nCr(n-1, 1, p-1));

 if(p<1)
 return (n/r)*(nCr(n-1, r-1, 1));

 return ( n/(p*r) )*nCr(n-1, r-1, p-1);
}

© Stack Overflow or respective owner

Related posts about c

    Related posts about recursion