Binomial Coefficient

I need help with this:

For given integers n and r calculate the binomial coefficient with this formula:
[n above r] = r!/(n-r)!*r!
Write the read numbers and the result.
Write the main program which should:
-Load numbers n and r from interval [2, 10]. The loading should contiunue until the allowed number is read from the interval.
-Calculate the factorial using the function.

I've finished the part with integers but I don't know how to implement them
in a code.
P.S. I've recently just begun to program in my school.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
using namespace std;

int calculating ()
{
    int n=1;
    int r=10;
do{if(r==10)
n++;
cout<<"n je " << n << endl;
if (r==10)
r-=9;
if (r<10)
r++;
cout << "r je " << r <<endl;

}while(n<10 || r<10);
}
It sounds like you need to step back and think about how to break the problem into manageable parts. Then, each part will be easier to solve.

First off, the equation you must implement contains factorials (r!). So, your first step should be to calculate a factorial. This would be a candidate for a function.

Next, given values for n and r, you need to calculate the binomial coefficient from the above equation, using the factorial function I just discussed. That should be pretty straight forward.

Last, you need to:
Load numbers n and r from interval [2, 10]. The loading should contiunue until the allowed number is read from the interval.
I'm not sure what "loading" means here. It could mean reading user input or reading from a file. For testing purposes, or coding practice, you could write a couple of loops where r ranges from 2 to 10 and n ranges from r to 10, and call the binomial coefficient function with each set of values. Once you get that working, you can modify your approach to get the r and n values from some other source (user input, etc.).
Last edited on
Topic archived. No new replies allowed.