Probability of A winning the match when individual probabilities of hitting the target given in C++


Given with two players let’s say A and B both are trying to get a penalty for winning the match. Given with four integer variables a, b, c, d so the probability of A getting the penalty first is a / b and probability of B getting the penalty first is c / d.

The one who scores the penalty first will win the match and as per the given problem statement program must find the probability of A winning the match.

Input 

a = 10, b = 20, c = 30, d = 40

Output 

probability is 0.5333

Input

a = 1, b = 2, c = 10, d = 11

Output 

probability is 0.523

Approach used in the below program is as follows

  • Input the values of four integer variables a, b, c, d

  • Subtract the probability of B winning the match from the total probability and we will get the resultant probability of A winning the match

    e * (1 / (1 - (1 - f) * (1 - f))))

    Where, e is the probability of A winning the match and f is the probability of B winning the match

  • Display the probability of A winning the match

Algorithm

Start
Step 1→ Declare function to calculate the probability of winning
   double probab_win(int a, int b, int c, int d)
      Declare double e = (double)a / (double)b
      Declare double f = (double)c / (double)d
      return (e * (1 / (1 - (1 - f) * (1 - f))))
Step 2→ In main()
   Declare variable as int a = 10, b = 20, c = 30, d = 40
   Call probab_win(a, b, c, d)
Stop

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
// calculate the probability of winning the match
double probab_win(int a, int b, int c, int d){
   double e = (double)a / (double)b;
   double f = (double)c / (double)d;
   return (e * (1 / (1 - (1 - f) * (1 - f))));
}
int main(){
   int a = 10, b = 20, c = 30, d = 40;
   cout<<"probability is "<<probab_win(a, b, c, d);
   return 0;
}

Output

If run the above code it will generate the following output −

probability is 0.5333

Updated on: 13-Aug-2020

58 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements