Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Binomial Coefficients In probability and statistics applications, you often

ID: 3680493 • Letter: C

Question

C++

Binomial Coefficients In probability and statistics applications, you often need to know the total possible number of certain outcome combinations. For example, you may want to know how many ways a 2-card Blackjack hand can be dealt from a 52 card deck, or you may need to know the number of possible committees of 3 people that can be formed from a 12-person department, etc. The binomial coefficient (often referred to as "n choose k") will provide the number of combinations of k things that can be formed from a set of n things. The binomial coefficient is written mathematically as: which we refer to as "n choose k". Binomial coefficients can be defined recursively: Individually, write a recursive function named choose (int n, int k) that will compute and return the value of the binomial coefficient. Then compare your function to your partner's, and together (i) come up with a function implementation you both agree on, and (ii) write it as a C++ function on the computer.

Explanation / Answer

#include <iostream>

using namespace std;

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


int main(int argc, char **argv)
{
   cout << "binomial(2, 52) is " << binomial(2, 4) << endl;
   cout << "binomial(3,12) is " << binomial(10, 3) << endl;

   cout << endl << "Press any key to continue." << endl;
   getchar();

   return 0;
}