crecursion

Calculating binomial coefficient using recursion in C


I am trying to build a program to calculate the binomial coefficient using the recursion formula. However, when I enter k = "n", the program shows "11db" in blue, which I think is a kind of error, maybe?

The code is shown below:

#include <stdio.h>

int Cnk(int n,int k);
int main()
{
 unsigned int n,k,result;
 printf("\n Please enter n = ");
 scanf("%d",&n);
 printf("\n Please enter k = ");
 scanf("%d",&k);
 result=Cnk(n,k);
 printf("\n %d\n",result);
 return 0;
}

int Cnk(int n,int k)
{
if(k==0 || k==n)
    return 1;
return Cnk(n-1,k-1)+Cnk(n-1,k);
 }

The results whenever I enter k = "n"

screenshot with text; Please enter n = 15 Please enter k = n (lldb)

The picture of text shows:

Please enter n = 15
Please enter k = n
(lldb)

The last line is blue and has lower case "L"s. I am trying to get the output "1" when I type in k = "n". I am confused as to what is wrong with my code.

Any help would be greatly appreciated.


Solution

  • Your code should actually work, at least concerning clear output (though heeding pmgs recommendations would be a good practice to establish for your habits...). The problem is your input.
    If you program to read a number, with either "%d" or "%u", then your program cannot take a non-numeric character like "n".
    You probably expect some kind of variable replacement, but the program does not actually see a relation between the character "n" in input and the value of one of the variables. The fact that the variables identifier happens to be n before compiling is irrelevant here.

    Your program as is needs a numeric specific value in input, i.e. if you entered 2 for n and want k to also be 2, then you need to enter "2", not "n".

    Writing a program which can actually do the desired variable replacement is (please excuse my guess here) complex beyond your current understanding of programming.

    In case you want to know more about the weird output you get (which I actually doubt) let me mention this:

    After scanf("%d",&k); fails because of non-numeric input, using k inside result=Cnk(n,k); causes undefined behaviour because of non-initialised variable k being used. It is important to read up on that concept. For your case it means that absolutely anything could theoretically happen, inlcuding any level of weird output. A quote from "Alice in Wonderland", in rainbow colors would be possible. Theoretically. Undefined Behaviour is really something to avoid.

    I had a suspicion that your environment is more likely to make that colored output, but could not point my finger at somethign plausible. Luckily, rici contributed very helpful insight:

    it's likely that the blue (lldb) prompt might be coming from the lldb debugger

    Until now, this answer focuses only on the unexpected output format or lack of output. As noted, the program does produce output of the expected format, if given suitable input.
    However, ryyker found cases in wich the program actually has algorithmic problems:

    if the 2nd value is bigger than the first, one goes negative before the other can catch up, and the only possible exit conditions are never met.
    For n==2 and k==4 as inputs, the code goes into an infinite loop. The exit criteria for the recursive function needs to be fixed.