Hello all!! I've been working on this code for a while now and I'm still lost. Can anyone tell me what's wrong with this code please???
I know Line 44 is missing something but I'm not sure what goes there, here is the pseudocode as well:

/* 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
}

#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 = ; //Write inital value
      
      for ( i = n : <= k ; i = i + 1 )
      denominator = sum * 1 ;
      numerator = (n-k+1)*(n-k+2)*...*(n-1)*n=
      (n-(k-1))*(n-(k-2))*...*(n-1)*(n-0)=
      (n-0)*(n-1)*... (n-(k-2))*(n-(k-1))

     
     return (numerator / denominator) ; 
}

Dani AI

Generated

The immediate cause of the crash in your posted code is a missing initialization and an invalid loop. was right that separate loops will work, but a safer, simpler method is the multiplicative approach: compute C(n,k) by looping k steps and multiplying/dividing at each step. This avoids building huge numerator/denominator products and is easy to implement and test.

A compact, robust C++ implementation (use 64-bit integers for moderate n):

#include <algorithm>
#include <cstdint>

std::int64_t binomial(int n, int k) {
    if (k < 0 || k > n) return 0;
    k = std::min(k, n - k);         // symmetry: compute the smaller side
    std::int64_t res = 1;
    for (int i = 1; i <= k; ++i) {
        res = res * (n - k + i) / i; // multiply then divide keeps values smaller
    }
    return res;
}

Why this works and what to watch for:

  • Reducing k to min(k, n-k) cuts the loop iterations and intermediate sizes.
  • The integer division at each step is exact for binomial coefficients, so no rounding loss occurs.
  • Use a 64-bit integer (long long / int64_t); results still overflow for sufficiently large n (roughly n > 60 for central coefficients). For larger ranges, use a bigint library such as Boost.Multiprecision.
  • If you prefer the two-loop product approach mentioned by , initialize the accumulators to 1 and use correct C++ loop syntax (for (i = 1; i <= k; ++i)), and reduce by gcd during multiplication to avoid overflow.

Further reading on definitions and alternative methods is available at the Binomial coefficient reference: Binomial coefficient.

Recommended Answers

All 2 Replies

use separate loops to calculate the numerator and denominator.

Thanks! Can you give me an example of what you mean and specify the line to which you are referring?

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.