C++/Im lost

Advertisement


Question
I have taken an online course and thus found out for myself that im not all that good with computers, at least not without being taught face to face and not on line.  Here is one of the programs that is giving me trouble, any help would be appreciated.  the first is the code template followed by however i have butchered it.
(p4) Project 4 - Binomial Numbers
In creating your program, carefully copy the given template exactly, maintaining all the blank lines, spaces, and general alignment. Then replace the areas that have been highlighted in yellow with your code. Do NOT change any of the other code.

Once you have a complete program, you should execute the code and submit your source code to your instructor.

You do not have to understand all the lines of code that have already been written, but try to compare the algorithms and the programs, and appreciate for yourself the fact that the programs are a straightforward realization of the algorithms.

This programming exercise requires you to write a function that computes binomial numbers.

Task
Binomial numbers (or binomial coefficients) are important positive integers that arise frequently in computing and mathematics. A function to compute binomial coefficients will have two inputs: n and k. The corresponding coefficient is written as nCk.

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

Here are some additional notes:

Because binomial numbers are integers, the return type should be int, and the two parameters, n and k, are also integers.
We have used the notation
1*2*…*k

to indicate the product of consecutive numbers. For example,

1*2*…*5

is actually

1*2*3*4*5 = 120

Similarly,

6*7*...*10 = 6*7*8*9*10 = 30,240

Part of the challenge in this exercise is to figure out how to compute this product. You should review the for loop in module 3, and recall how for loops are written in C++. Also review the programming exercise on exponentiation in module 3.
You can use integer division in line 7 above—it turns out that if numerator and denominator are computed as shown above, denominator will always evenly divide numerator.
To test this function, you must write a driver (in the code template given below, the driver is the main function).

You should design a suitable test suite and test your program using it. The test suite is part of the materials that you will be submitting.

Code Template for Exercise 1
/****************************************************/
/* File: name of your file with the source code */
/* */
/* Created by: give your name */
/* Date: give the date */
/* */
/* 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 = Write the function call ;

cout << "Binomial number " << n << "C" << k
<< " = " << result << endl ;

return (0) ;
} // end main


// ********************************************************

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

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
} // end if
} // end function binomial

---------------------------my masterpiece----------------------
/****************************************************/
/* File: name of your file with the source code */
/* */
/* Created by: Andrew Myers */
/* Date: Nov 09, 2009 */
/* */
/* 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 = binomial (n,k) ;

cout << "Binomial number " << n << "C" << k
<< " = " << result << endl ;

return (0) ;
} // end main


// ********************************************************

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

if (n<k) then
{
return(0) ;
}
else
{
denominator = k ;

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

return (n) ;
} // end if
} // end function binomial  

Answer
your masterpiece has most of the things correctly done, except

change
#include < iostream >  

to
#include <iostream> // no spaces around names of headers

To compute the denominator of the binomial coeffient, ie  denominator = (n-k+1)*(n-k+2)*...*(n-1)*n,
 a. start with denominator = 1
 b. write a loop which varies i from 1 to k inclusive ie. i == 1, 2, 3, 4, 5, 6 ..... k
 c. inside the loop, multiply the current denominator by i


To compute the numerator of the binomial coeffient, ie  numerator = (n-k+1)*(n-k+2)*...*(n-1)*n,
 a. start with numerator = 1
 b. write a loop which varies i from 1 to k inclusive ie. i == 1, 2, 3, 4, 5, 6 ..... k
 c. inside the loop, multiply the current numerator by ( n - k + i )

finally return numerator / denominator ;

oh, btw, the if statement does not have a 'then' clause, it is implied

if( condition )
{
}
else
{
}

The binomial function at the end should look:

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

 if (n<k) // then ****** there is no keyword 'then'
 {
   return(0) ;
 }
 else
 {
       // Set denominator = 1*2*...*k
       denominator = 1 ;

       for ( i = 2 ; i <= k  ; i = i+1 ) // for i = 1, 2, 3, 4, 5, 6 .. k
         denominator = denominator * i  ;

       // Set numerator = (n-k+1)*(n-k+2)*...*(n-1)*n
       numerator = 1 ;
      for ( i = 1 ; i <= k ; i = i+1 ) // for i = 1, 2, 3, 4, 5, 6 .. k
         numerator = numerator * ( n - k + i ) ;

      return numerator / denominator ;
 } // end if
} // end function binomial  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.