0

1.I just understood by calculating one binomial coefficient example step by step. but when it comes to pseudo code, I am a little bit confused. especially this part for(j=minimum(i,k); j>=0; j--).
and what else I understood is that n is the size of column, and k is the size of row. I want to know if I understood right or not.

int bin2(int n, int k)
{
    index i, j;

    int B[0...k];

    for(i = 0;i <= n; i++)
    {
        for(j = minimum(i, k); j >= 0; j--)
        {
            if(j == 0 || j == i)
                B[j]=1;
            else 
                B[j] = B[j - 1] + B[j]; 
        }
    }

    return B[n][k];
}
Daniel Corzo
  • 1,055
  • 2
  • 19
  • 32
jay
  • 55
  • 1
  • 8
  • 2
    `B[n][k]` doesn't make sense if `B` is a 1-dimensional array. If you are trying to build up Pascal's triangle you need a 2-d array. – John Coleman Dec 09 '15 at 17:33
  • Also -- calculating a binomial coefficient by calculating all of Pascal's triangle above it (which you seem to be trying to do) is inefficient. There are recurrences which allow one to calculate the nth row without calculating previous rows. See http://stackoverflow.com/q/15580291/4996248 – John Coleman Dec 09 '15 at 17:49

1 Answers1

0

That's a c style loop. What it says is iterate with j from minimum(i,k) down to 0 (or as long as j>=0 ) in steps of -1 (j-- decreases j by 1).

The return should read B[k] because it's a 1-dimensional array.

Sorin
  • 11,863
  • 22
  • 26