Jiskha Homework Help
SATURDAY
November 21, 2009
SCHOOL SUBJECTS
- Art
- Business
- Computers
- English
- Foreign Languages
- Health
- Home Economics
- Math
- Music
- Physical Education
- Science
- Social Studies
GRADE LEVELS
- Preschool
- Kindergarten
- Elementary School
- 1st Grade
- 2nd Grade
- 3rd Grade
- 4th Grade
- 5th Grade
- 6th Grade
- 7th Grade
- 8th Grade
- High School
- 9th Grade
- 10th Grade
- 11th Grade
- 12th Grade
- College
- Adult Education
Post a New Question | Current Questions | Chat With Live Tutors

Homework Help Forum: C++ Algorithms

Posted by plk on Thursday, April 9, 2009 at 12:52am.

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:

1. Because binomial numbers are integers, the return type should be int, and the two parameters, n and k, are also integers.

2. 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

3. 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.

4. 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) ;
}

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

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(??) ;
�� }
�� else
{
����� denominator = ?? ; // initial value

����� for (i = ?? ; i <= ?? ; i = i+1)
������� denominator = ?? * ?? ;

����� write code to compute numerator, along similar lines

����� return (??) ;
�� } // else
}





Anyone know how to do this? I got the compiler ready!

No one has answered this question yet.

Answer this Question

First Name:
School Subject:
Answer:

SEARCH

COMMUNITY
FEATURES
- Live Tutors
- Net Riddle
- Reference
- Search