programming languagae C, please. thanks 8. The mathematical combinations functio
ID: 3746936 • Letter: P
Question
programming languagae C, please. thanks
8. The mathematical combinations function C(n, k) is usually defined in terms of factorials, as follows: C(n , k) = k! ×(n-k)! The values of C(n, k) can also be arranged geometrically to form a triangle in which n increases as you move down the triangle and k increases as you move from left to right. The resulting structure,, which is called Pascal's Triangle after the French mathematician Blaise Pascal, is arranged like this: C(0,0) C,0) C1,1) C(2,0) C(2,1) C(2,2) C(3,0) C(3,1) C(3,2) C(3,3) C(4,0) C(4,1) C(4,2) C(4,3) C(4,4) Pascal's Triangle has the interestin entries above it, except along the le Consider, for example, the circled entry in the following display of Pascal's Triangle: ty that every entry is the sum of the two right edges, where the values are always 1 133 15 10 105 1 6 15 20 15 61 This entry, which corresponds to C(6,2), is the sum of the two entries-5 and 10- that appear above it to either side. Use this relationship between entries in Pascal's Triangle to write a recursive implementation of the Combinations function that uses no loops, no multiplication, and no calls to FactExplanation / Answer
To calculate C(n,k) or
combination(int n, int k)
By Pascal traingle use combination(n-1,k)+ combination(n - 1, k-1);
Hence write recursive function combination(int n,int k)
The function will be :
int combination(int n, int k)
{
//As if k>n then combination not defined hence return negative
if (k > n)
return -999999;
//If n==k combination is 1
else if (n == k)
return 1;
//If k==0 combination is 1
else if (k == 0)
return 1;
//If k==1 combination is n
else if (k == 1)
return n;
//Here Pascal Approach
else
return combination(n-1,k)+ combination(n - 1, k-1);
}
//The Whole program
#include<stdio.h>
int combination(int n, int k)
{
//As if k>n then combination not defined hence return negative
if (k > n)
return -999999;
//If n==k combination is 1
else if (n == k)
return 1;
//If k==0 combination is 1
else if (k == 0)
return 1;
//If k==1 combination is n
else if (k == 1)
return n;
//Here Pascal Approach
else
return combination(n-1,k)+ combination(n - 1, k-1);
}
//Test the program
int main() {
int a=5;
int b=2;
int z=combination(a,b);
printf("Result is %d", z);
return 0;
}
Output:
Result is 10
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.