Binomial Program errors

I am trying to write a program to calculate binomials, but I keep getting errors repeatedly. Any help would be nice. Here is my code:






#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)
{
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 = n - 1 + 1 ; i <= n; i= i+1)
numerator = numerator * i;

return (numerator / denominator) ;
}
}
Last edited on
You're going to need to tell us more than that. I don't want to spend time figuring out what the problem is on top of figuring out how to fix it.

Is the compiler giving you any errors? The linker? Did you try debugging it yourself? Any runtime errors? And please use [code][/code] tags.
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62


#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)
   {
     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);
   } 
}
denominator = 1*2*3*4*5*6*7*8*9*10*k ; //intitial value

for (i = k; i <= k ; i = i+1) ;
denominator = denominator * i ;

numerator = (n-k+1)*(n-k+2)*(n-k+3)*(n-k+4)*(n-k+5)*(n-k+6)*(n-k+7)*(n-k+8)*(n-k+9)*(n-k+10)*(n-1)*n ; // initial value

for (i = n ; i <= (n - k + 1) ; i ++)
numerator = numerator * (n-k+1) ;

return (numerator / denominator) ;

}
im not getting any errors either but test plan my professor gave does not match my out put any ideas why

and i was told the problem might be with my termination condition
Last edited on
@kinneyj: You're doing n! / k!

@hobro1: ¿what are you doing? your denominator is 10! * k2
not sure what you are talking about plz elaborate using the code i have above
What mathematical equation are you basing your program on?

I'm not sure you've coded the whole of the binomial equation (assuming you were planning to use the standard factoral form)

Ah yes - I agree with ne555's comment.

Start off by sorting out the factorial part!

Andy
Last edited on
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) ;

return (numerator / denominator) ;
}







}
Last edited on
Can you write the equation you want to solve, using the style ne555 used about?
Topic archived. No new replies allowed.