Browse Source

add binomial_coefficient

main
fdai6887 2 years ago
parent
commit
7439a808da
  1. 12
      src/funktion.c
  2. 1
      src/funktion.h
  3. 5
      src/main.c

12
src/funktion.c

@ -253,3 +253,15 @@ double probability_from_tree(double successful_outcomes, double total_outcomes,
result = successful_outcomes / (total_outcomes * branches); result = successful_outcomes / (total_outcomes * branches);
return result; return result;
} }
int binomial_coefficient(int n, int k) {
int result = 1;
if (k > n - k) {
k = n - k;
}
for (int i = 0; i < k; ++i) {
result = result * (n - i) / (i + 1);
}
printf("The binomial coefficient C(%d, %d) is %d\n", n, k, result);
return result;
}

1
src/funktion.h

@ -42,5 +42,6 @@ float Vskalort(float x1, float x2, float x3, float z1, float z2, float z3);
double Vangle(float x1, float x2, float x3, float z1, float z2, float z3); double Vangle(float x1, float x2, float x3, float z1, float z2, float z3);
float vPunkt(float x1, float x2, float x3, float z1, float z2, float z3, float p1, float p2, float p3); float vPunkt(float x1, float x2, float x3, float z1, float z2, float z3, float p1, float p2, float p3);
double probability_from_tree(double successful_outcomes, double total_outcomes, double branches); double probability_from_tree(double successful_outcomes, double total_outcomes, double branches);
int binomial_coefficient(int n, int k);
#endif #endif

5
src/main.c

@ -200,4 +200,9 @@ int main()
y = getValue('T'); y = getValue('T');
z = getValue('B'); z = getValue('B');
probability_from_tree(x, y, z); probability_from_tree(x, y, z);
printf("Enter a and b to calculate the binomial coefficient: ");
a = getValue('a');
b = getValue('b')
binomial_coefficient(a, b);
} }
Loading…
Cancel
Save