Hello everyone!!!

I have the following pseudocode and I'm not exactly sure how/where to plug it into my code. I am still very lost concerning programing so please bear with my ignorance...:)

/* pseudocode for Binomial
Coefficients */
int binomial(int n, int k)
{
If (n < k) Then return (0)
Else
{
Set denominator = 1*2*...*k
Set numerator = (n-k+1)*(n-k+2)*...*(n-1)*n
return (numerator / denominator)
} // else
End if
}


This is my code below but from line 38, I'm not sure what to do next:

#include <iostream>

using namespace std ;

int binomial(int n, int k) ; // function prototype


int main ()
{
    
    int n, k ; // parameters for the binomial number
    int result ;
    
    cout << endl ;
    
    // read in n & k
    
    cout << "Enter n (positive integer) : " ;
    cin >> n ;
    
    cout << "Enter k (positive integer) : " ;
    cin >> k ;
    
    result = binomial(n,k); 
    
    cout << "Binomial number " << n << "C" << k
         << " = " << result << endl ;
         
    return (0) ;
}

int binomial(int n, int k)

{
    int numerator, denominator ;
    int i ; // needed to compute numerator & denominator
    
    if (n < k) Then
{
    return (0) ; 
}
  else
  {
      denominator = 1*2*...*k ; 
      
      for ( i = : <= ; i=i+1)
     denominator = * ;
     write code to compute numerator, along similar lines
     
     return ( ) ; write return value
}

Dani AI

Generated

A few practical points to finish the function and avoid the common pitfalls that trip beginners (and what to do instead of copying the pseudocode verbatim).

As wrote, the test should be if (n < k) and return 0. is right that two loops (one for numerator, one for denominator) will produce the correct answer, but that naive factorial-style approach easily overflows for modest n. A safer, standard trick is to compute the coefficient multiplicatively in a single loop and always use the smaller k = min(k, n-k). This keeps intermediate values far smaller and runs fewer iterations.

Example implementation (different from the posted pseudocode):

long long binomial(int n, int k) {
    if (n < 0 || k < 0) return 0;
    if (k > n) return 0;
    if (k == 0 || k == n) return 1;
    if (k > n - k) k = n - k;
    long long res = 1;
    for (int i = 1; i <= k; ++i) {
        res = res * (n - k + i) / i;
    }
    return res;
}

Why this works: it effectively multiplies the numerator factors (n-k+1 ... n) one at a time and divides by the current i, so each division is exact. Quick worked example: n=5, k=2 -> steps give 4 then 10.

Cautions: use a 64-bit type (long long) for larger results, validate inputs (negative numbers), and remember even 64-bit will overflow for large n; for very large values use a big-integer library such as Boost.Multiprecision. Test edge cases: k==0, k==n, k>n. This lets you replace the red/missing lines safely and gives a robust binomial implementation.

Recommended Answers

All 4 Replies

Yeah, from line 38 everything seems to be messed up. Could you please explain more about the binomial number please. What exactly are you supposed to find?

you will need to set up the variable values for numerator and the denominator using 2 different for loops

and then compute numerator/denominator.. after which you send it up.

Hi, This is the code starting at line 38, I am supposed to plug my code into the red areas but I'm very lost, the pseudocode is in the beginning of my original post.....Thanks!

if (  ) Write if-test
   {
     return(  ) ; Write return value
   }
   else 
   {
      denominator =    ; Write initial value

      for (i =    ; i <=    ; i = i+1)
        denominator =    *    ;
        Write code to compute numerator, along similar lines

      return (  ) ; Write return value
   }
}

Thanks for replying. The thing is that I'm not exactly sure how to do that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.