HELP WITH C++ HOMEWORK Create a program that uses a function to compute the bino
ID: 3913932 • Letter: H
Question
HELP WITH C++ HOMEWORK
Create a program that uses a function to compute the binomial coefficients (the number of combinations k items that can be selected from a set of n items.)
The binomial coefficient is defined as:
n/k = C(n,k) = n!/k!(n-k)! for n and k satisfying : 0 < or equal k < or equal n
- If n < 0, k < 0 or K > n the binomial coefficient is considered to be zero.
(Both n and k are integer numbers)
- Also, 0/0 = 1 the one example where the recursion relation does not hold.
Prompt the user for the input values of n and k respectively.
Create a function named factorial that will compute the factorial of a number.
3. Create a function named binomial to compute the binomial coefficients using the inputs entered by the user. You must use the factorial function defined from part 2.
4. Create a function named output to print the binomial coefficients or an error message to the screen.
5. The output should look like the one of following:
The binomial coefficient C(7, 3) = 35
The binomial coefficient C(3, 7) = 0 (Invalid parameters)
The binomial coefficient C(4,-1) = 0 (Invalid parameters)
6. Test your program with the following inputs of n and k.
(12,3), (3,4), (4,-2), (-4,7), (13,0)
Expected output for each of the values required:
(12, 3): BC = 220
(3, 4): BC = 0, Invalid
(4, -2): BC = 0, Invalid
(-4, 7): BC = 0, Invalid
(13, 0): BC = 1
----------------------------------------------------------------------------------------------------------------------------------------------
what I have so far ( I don`t know if my function factorial is correct, so fix if it is necessary)
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int factoria (int n);
int binomial (int n, int k);
int output (binomial);
int main()
{
int n;
int k;
cout << "Enter a positive number for n: ";
cin >> n;
cout << "Enter a positive number for k that is smaller or equal n: ";
cin >> k;
return 0;
}
int factorial (int n)
{
if (n == 1)
return n;
else
return n * factorial (n-1);
}
int binomial (int n, int k)
{
}
int output (binomial)
{
}
Explanation / Answer
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int factoria (int n);
int binomial (int n, int k);
int output (int n, int k);
int main()
{
int n;
int k;
cout << "Enter a positive number for n: ";
cin >> n;
cout << "Enter a positive number for k that is smaller or equal n: ";
cin >> k;
int result = output(n, k);
if(result == 0)
cout << "Result is 0, Invalid";
else
cout << "Result is " << result;
return 0;
}
int factorial (int n)
{
if (n == 1 || n==0)
return 1;
else
return n * factorial (n-1);
}
int binomial (int n, int k)
{
return (factorial(n)/(factorial(k)*factorial(n-k)*1.0));
}
int output(int n, int k)
{
if(n<k || n<0 || k<0)
return 0;
return binomial(n, k);
}
/* SAMPLE OUTPUTS
Enter a positive number for n: 7
Enter a positive number for k that is smaller or equal n: 3
Result is 35
Enter a positive number for n: 3
Enter a positive number for k that is smaller or equal n: 7
Result is 0, Invalid
Enter a positive number for n: 4
Enter a positive number for k that is smaller or equal n: -1
Result is 0, Invalid
Enter a positive number for n: 12
Enter a positive number for k that is smaller or equal n: 3
Result is 220
Enter a positive number for n: 3
Enter a positive number for k that is smaller or equal n: 4
Result is 0, Invalid
Enter a positive number for n: 4
Enter a positive number for k that is smaller or equal n: -2
Result is 0, Invalid
Enter a positive number for n: -4
Enter a positive number for k that is smaller or equal n: 7
Result is 0, Invalid
Enter a positive number for n: 13
Enter a positive number for k that is smaller or equal n: 0
Result is 1
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.