8000 feat: added addition_rule.cpp for probability (#732) · SelfCodeLearning/C-Plus-Plus@f6dd3bf · GitHub
[go: up one dir, main page]

Skip to content

Commit f6dd3bf

Browse files
RobotRagecclauss
andauthored
feat: added addition_rule.cpp for probability (TheAlgorithms#732)
* added addition_rule.cpp for probability * fixed cpplint errors * Disable actions-gh-pages Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 4131963 commit f6dd3bf

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

probability/addition_rule.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <iostream>
2+
3+
// calculates the probability of the events A or B for independent events
4+
5+
double addition_rule_independent(double A, double B) {
6+
return (A + B) - (A * B);
7+
}
8+
9+
// calculates the probability of the events A or B for dependent events
10+
// note that if value of B_given_A is unknown, use chainrule to find it
11+
12+
double addition_rule_dependent(double A, double B, double B_given_A) {
13+
return (A + B) - (A * B_given_A);
14+
}
15+
16+
int main() {
17+
double A = 0.5;
18+
double B = 0.25;
19+
double B_given_A = 0.05;
20+
21+
std::cout << "independent P(A or B) = "
22+
<< addition_rule_independent(A, B) << std::endl;
23+
24+
std::cout << "dependent P(A or B) = "
25+
<< addition_rule_dependent(A, B, B_given_A) << std::endl;
26+
27+
return 0;
28+
}

0 commit comments

Comments
 (0)
0