Skip to content

Commit

Permalink
Finishing up to Q3 in lab 5 UofUEpiBio/PHS7045-advanced-programming#29
Browse files Browse the repository at this point in the history
  • Loading branch information
gvegayon committed Sep 19, 2024
1 parent 096dd0c commit eff77c5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
5 changes: 5 additions & 0 deletions lab5/binom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ int main() {

}

std::cout << "Q3 dbinom" << std::endl;
for (int i = 0; i < 11; i++) {
std::cout << b.dbinom(i) << std::endl;
}

return 0;

}
29 changes: 19 additions & 10 deletions lab5/binom.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,41 @@ class Binom {
double p;

public:
Binom(int n, double p) : n(n), p(p) {};
int factorial(int n) const;
// Binom(int n_, double p_) : n(n_), p(p_) {};
Binom(int n_, double p_) {
n = n_;
p = p_;
};
int factorial(int k) const;
double choose(int a, int b) const;
double dbinom(int k) const;
void print(int k) const;
};


inline int Binom::factorial(int n) const {
inline int Binom::factorial(int k) const {

if (n <= 1)
if (k <= 1)
return 1;

return this->factorial(n - 1) * n;
return this->factorial(k - 1) * k;

}

inline double Binom::choose(int a, int b) const {

// double a_dbl = (double) a;
// double b_dbl = static_cast<double>(b);

return this->factorial(a)/(
factorial(a - b) * factorial(b)
return static_cast<double>(this->factorial(a))/(
static_cast<double>(factorial(a - b)) *
static_cast<double>(factorial(b))
);

}

inline double Binom::dbinom(int k) const {

return choose(n, k) *
std::pow(p, k) * std::pow(1 - p, n - k);

}

#endif

0 comments on commit eff77c5

Please sign in to comment.