You are on page 1of 3

#include <iostream>

//Analysis of Algorithms Assignment Submitted by Siddharth Bharti Rollno. BT07CS


E022
#define n 5 //No. of Games played 2n-1 ie 63 matches.
#define p 0.5 //Probability that Team A will be the winner
#define q 0.5 //Probability that Team A will be the winner
using namespace std;
/* Problem Statement */
//Imagine a competiton in which two teams A & B play not more than 2n-1 games, t
he winner being the first team
//to achieve n victories. Assume that there are no tied games, that the results
of each match are independent
//and that for any given match there is a constant probability p that team A wil
l be the winner, and hence a
//constant probability q=1-p that team B will win.
//team A wining probability => p
//team B wining probability => q
//P(i, j) => probability that the Team A will win if it gets i more victories an
d the team B will win if it gets j more victories.
//If team A has already won all the matches it needs, then it is of course certa
in that they will win the series:
// P(0, i) = 1, 1 <= i <= n
//If team B has already won all the matches it needs, then it is of course certa
in that they will win the series:
// P(i, 0) = 0, 1 <= i <= n
//To Calculate the probability P(i,j)
// P(i, j) = pP(i-1,j) + qP(i,j-1); i >= 1; j >= 1;
float pij(int , int);
// pP(i-1,j)=p*probability that team A has already won i-1 matches and team B ha
s won j matches.
// qP(i,j-1)=q*probabilty that team A has already won i matches and team B has w
on j-1 matches.
class Team
{
private:
int wins;
float winingChance;
public:
Team() // Default Const
ructor Takes Value from User
{
int w;
cout<<"Enter Total no. of matches the team has already won :
";
cin>>w;
cout<<"\n";
if(w<=n)
{
wins=w;
}
else
{
cout<<"Enter no. of matches won less than "<<n<<"\n\n";
cout<<"Enter Total no. of matches the team has already
won :";
cin>>w;
wins=w;
cout<<"\n";
}
}
int getWins() //Get
No. of matches already won by the team
{
return wins;
}
float getwiningChance() //Get
Wining Chance of The Team
{
return winingChance;
}
void setWiningChance(float winChance) //Set
ting Wining Chance of The Team
{
winingChance=winChance;
}
};
float pWin(Team &A, Team &B)
{
float tempA;
int needA,needB;
needA=n-A.getWins();
needB=n-B.getWins();
tempA=pij(needA,needB);
return tempA;
}
float pij(int Aneeds, int Bneeds)
{
if (Aneeds == 0)
{
return 1;
}
else if (Bneeds == 0)
{
return 0;
}
else
{
return (p*pij(Aneeds-1,Bneeds) + q*pij(Aneeds,Bneeds-1));
}
}

int main()
{
int select;
float temp;
cout<<"For Team A :\n\n";
Team A;
cout<<"For Team B :\n\n";
Team B;
temp=pWin(A,B);
A.setWiningChance(temp);
cout<<"\n"<<"Wining Chance of Team A is :"<<A.getwiningChance()<<"\n";
system("PAUSE");
return 0;
}

You might also like