No errors from the compiler, and I have debugged it myself. No runtime erros either. I changed up one of the for loops, and now it will calculate the numerator, but not the denominator and give that as an answer. The new code is:
#include <iostream>
usingnamespace 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)
{
return (0);
}
else
{
denominator = 1; //initial value of denominator
for (i = 1; i <= k; i= i+1)
denominator = denominator * i;
numerator = 1; //initial value of numerator
for (i = 1; i <= n; i= i+1)
numerator = numerator * i;
return (numerator / denominator);
}
}
binomial equation the same as kennyj this is part of the program that im having trouble with for loop statements that are supposed to compute the binomial factors of two numbers n as the numerator and k as the the denominator.
Two positive integers (n & k) */
/* */
/* Output: */
/* Corresponding binomial coefficient (nCk) */
/* */
/* Algorithm: see attached description */
/****************************************************/
#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 ;
system ("pause") ;
return (0);
}
int binomial (int n , int k)
{
int numerator, denominator ;
int i ; // needed to compute numerator & denominator
if ( n < k ) // test if n is less than k
{
return (0) ;
}
else
{
denominator = 1 ; //intitial value
for (i = 1; i <= k ; i = i+1) ;
denominator = denominator * i ;
numerator =1 ; // initial value
for (i = 1 ; i <= n - k + 1 ; i = i+1);
numerator = numerator * (n-k+1) ;