Total members 9951 | Gratitudes |It is currently Sat Feb 11, 2012 8:57 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 4 posts ] 
Author Question
 Question subject: compute binomial coefficients
PostPosted: Thu Apr 23, 2009 1:36 am 
Offline
Newbie
User avatar

Joined: Thu Apr 09, 2009 2:39 am
Posts: 3
Has thanked: 0 time
Have thanks: 0 time

I have to take c++ as part of my degree plan but I can't figure out this language. Can anyone help with this code PLEASE?
Code:
/* Program to compute binomial coefficients         */
/*                                                  */
/* Inputs: (keyboard)                               */
/*   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 =  NEED TO WRITE FUNCTION CALL HERE
   cout << "Binomial number " << n << "C" << k
        << " = " << result << endl ;
   return (0) ;
}
// ********************************************************
int binomial(int n, int k)
/* Computes the binomial coefficient nCk */
/*                                       */
/* Inputs:                               */
/*    n, k (integers)                    */
/*                                       */
/* Output:                               */
/*    binomial coefficient nCk (integer) */
{
   int numerator, denominator ;
   int i ; // needed to compute numerator & denominator
   
ALL THIS STUFF NEEDS TO BE CHANGED

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


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: compute binomial coefficients
PostPosted: Sat Aug 08, 2009 7:21 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2103
Location: Earth
Has thanked: 39 time
Have thanks: 56 time
What is your problem in this code . it is C++ for compute binomial coefficients .

which to run you need to replace
NEED TO WRITE FUNCTION CALL HERE

with

binomial(n,k) .

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: compute binomial coefficients
PostPosted: Fri Aug 14, 2009 4:19 am 
Offline
Newbie
User avatar

Joined: Fri Aug 14, 2009 4:13 am
Posts: 1
Has thanked: 0 time
Have thanks: 0 time
we define the binomial coefficients by:

(x+y)^n = sum(k=0,n, nCk x^(n-k) y^k)

and we want to show that:

nCk = (n!)/(n-k)!k!

is true. So let's use induction on n.

Base case: n = 1. (x+y)^1 = x+y, so 1C0 = 1C1 = 1!/1!0! = 1!/0!1!.

Inductive step: suppose nCk = (n!)/(n-k)!k! for some n and all 0 ≤ k ≤ n. We'll show it's true for n+1 and all 0 ≤ k ≤ n+1. So

(x+y)^(n+1) = (x+y)(x+y)^n = (x+y)sum(k=0,n, nCk x^(n-k) y^k)

So multiply through and note that the coefficient on x^(n+1-k) y^k is:

for k = 0: 1 = (n+1)!/(n+1)!0!
for k = n+1: 1 = (n+1)!/0!(n+1)!
for 1 ≤ k ≤ n: nCk + nC(k-1)

So we want to show that nCk + nC(k-1) = (n+1)!/(n+1-k)!k!. So:

n!/(n-k!)k! + n!/(n+1-k)!(k-1)! = [n!(n+1-k)+n!k]/(n+1-k)!k! = n!(n+1)/(n+1-k)!k! = (n+1)!/(n+1-k)!k!


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: compute binomial coefficients
PostPosted: Fri Aug 14, 2009 12:29 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2103
Location: Earth
Has thanked: 39 time
Have thanks: 56 time
good information .

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 4 posts ] 
Quick reply


  


 Similar topics
 Topic title   Forum   Author   Comments 
 compute area of the circle.  Java  Anonymous  1

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 2 guests



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team