it says two teams A & B have to play a match, and we have to find probability of team A. I can't understand the way to calculate its percentage. The main points are:

1) the match is played by two teams: A and B;

2) the first team to win K games in a match, wins the match;

3) each game consists of rounds; each round is won by one of the teams, and the team adds one point to its score for the current game;

4) the first team to win L points in a game, wins the game;

5) if team A serves the ball in a round, it has Pa% chance to win the round (and (100-Pa)% to loose it);

6) if team B serves the ball in a round, it has Pb% chance to win the round (and (100-Pb)% to loose it);

7) if a round is not the first in a game, the ball in the round is served by the team that won the last round;

8) if a game is not the first in a match, the ball in the first round of the game is served by the team that did not served the ball in the first round of the previous game;

9) both teams have an equal chance to serve the ball in the first round of the very first game of a match.

Sample Input

2 \\data sets
100 50 1 3\\values for Pa,Pb,K,L
100 1 1 1\\same as above

Sample Output \\this is wat i cant calculate

93.8
99.5

I m a beginner and need help so plz just let me know how can I do it?


It probably has many if conditions, but how many is the Question??

Dude Post in some of the code and then be specific in the questioning . This looks like you are trying to get the homework done totally by the members.

>>It probably has many if conditions, but how many is the Question??

This basically depends upon the programmer.

For example.

instead of

if(a==1)
{
if(b==2)
{
cout<< right;
}
}

A programer can choose

if(a==1&&b==2)
{
cout<<right;
}

It isnt related to your problem but its just an example.

Thanx alot but can you help me with that??

Thanx alot but can you help me with that??

Sure, Post in the code and then ask where exactly you need help. I mean you must have written the code till some extent right.

OK I did one case for you, hopefully you can derive a more general case from it:

EDIT: they trim whitespace so nvm, words will have to do...I had a diagram for you...

Ok here we go: A has a 0.5 chance of serving first. If A serves, it has a Pa chance of winning. In this case, Pa = 1, so if A ever gets to serve, it will always win. Thus we have A will win 0.5 in this case (because there is a 0.5 chance that A will serve first).

If B serves first, then at each successive round, A has a 0.5 chance of getting to serve (B has a 0.5 chance of winning a round, and therefore of losing a round as well). Thus, there is a 0.5 chance that A will win the first round if B serves first, so then if A wins that round, it will win all of its next rounds, so that gives 0.25 chance of winning. (0.5 that B serves first multiplied by 0.5 that A wins the round).

If B wins the first, then A again has a 0.5 chance of winning the second round, and therefore has a 0.5*0.5*0.5 = 0.125 chance of winning if B wins the first round.

Using the same logic, A has a 0.5*0.5*0.5*0.5 = 0.0625 of winning if B wins the first 2 rounds.

If we add all this mess up, we get 0.5 + 0.25 + 0.125 + 0.0625 = 93.75 (or 93.8).

So now what you have to do, is at each step you need to generalize the case. That is, A will not always have a 100% chance of winning, it has a Pa% chance. That means that B could win some as well, so now you will have extra branches.

Basically what I would do to think of this is keep track of the number of rounds/games won by team A and B. It might be useful to think of this problem recursively (I've been doing a lot of Scheme programming, so recursion is now my friend :) ) -- of course, in C or C++, there is no optimization for recursion, you would have to write tail recursive functions, which is annoying in C/C++, so just think of the problem recursively and convert it to loops (thats what I would do anyway).

At each step you basically have two cases: team A win, or team B wins. There is also a probability associated with each step. So you must just define a function such that the probability is multiplied at each step, and you just keep following that process until you reach the desired number of rounds (L), and games (K)...

I think that was confusing, so let me know and ill try to clarify it a bit..

One thing was confusing that you did took out the required result, How does it relate to the input Pa,Pb,K,L??? Pa is entered in percent and we have to reduce it to simple as Pa/100, and Pb/100; explain me your output while using the input that is Pa=100,Pb=50,K=1,L=3;

K and L surely are used in loops for repetetions and Pa as you explained but how Pb acts?,

Thank you for the info that you provied me with!!!

One thing was confusing that you did took out the required result, How does it relate to the input Pa,Pb,K,L??? Pa is entered in percent and we have to reduce it to simple as Pa/100, and Pb/100; explain me your output while using the input that is Pa=100,Pb=50,K=1,L=3;

I was only doing one particular case... I wanted to give you a chance to try to extend it to the general case...

Let me try explaining like this: suppose we label each round played as a "node" (just for the sake of explanation). Now, at each node, there are 2 possible outcomes: either A wins, or B wins. So, if we think about this problem recursively, we only need to account for these two possible cases. However, the problem has a little more depth: there are 2 "types" of nodes. There is the node where A serves, and the node where B serves. Thus, at any particular node where A serves, there will be a Pa chance that A wins, and a (1-Pa) chance that B wins (note I'm using Pa and Pb as decimals not percents, same thing). Likewise, at any particular node where B serves, there is a Pb chance that B wins, and more importantly, a (1-Pb) chance that B loses (A wins). The same thing happens at each successive node. Since we are looking for the ways that A wins, all you need to do is connect every possible outcome where A wins L rounds (to make a game). That is, you could think of "connecting the nodes"...So each chain of connected nodes represents one possible way that A could win a round...You have add all of the chains together, which is what I did it my first post (hopefully this doesn't confuse you).

This will become more difficult if Pa does not equal 100%, so that is the challenge...That is where you have to construct the general case. This is because you could potentially have a case where A wins the first round, B wins the second round, A wins the third, B wins the fourth, and so on...

But don't let this frighten you! If we start with the basic case (like we have), we just need to build it into a more general case...

Also, in my example, I did not have to account for multiple games played, only multiple rounds (because the K value was 1). You will also need to account for this.

However, again, this will not be difficult once you have the basic stuff down. If you think about it, you can just repeat the entire process in successive games. So really, your focus should be on getting the right code for the rounds...

K and L surely are used in loops for repetetions and Pa as you explained but how Pb acts?,

I think I mentioned this in my above explanation.

Thank you for the info that you provied me with!!!

No problem! I love solving these anyway...it's all probability. I have to go for a bit, but I'll check back here later to see how everything is coming. I suggest, once you understand the case that I did, you should try constructing the second sample case. It will definitely help you understand the problem.

If your interested (and after you've solved the problem), you may want to check out a programming technique called "memoization"...It sort of relates to this problem, if you construct a solution in a particular way...(the way that I am thinking of). Goodluck :)

Plz Its hard for me i've already mentioned Im just a beginner and i 've been trying on this problem for so long, but i've failed. I really need that solved. Plz if you have time can you tell me how can I solve or can you do it as a favour??? I will be very thankfull to you!!! our current course is just for beginners whose main focus is on the syntax and code implementation at medium level, logic is no the part of my course yet. Plz can you help me with that??.. I have to submit it before 15th may and I really don't understand the problem uptill now....Plz if you have time can you do it,......

Plz Its hard for me i've already mentioned Im just a beginner and i 've been trying on this problem for so long, but i've failed. I really need that solved. Plz if you have time can you tell me how can I solve or can you do it as a favour??? I will be very thankfull to you!!! our current course is just for beginners whose main focus is on the syntax and code implementation at medium level, logic is no the part of my course yet. Plz can you help me with that??.. I have to submit it before 15th may and I really don't understand the problem uptill now....Plz if you have time can you do it,......

No, he can't do it for you. It's against the rules. Don't ask for any favors. It appears Sky Diploma was correct. You're looking to just post and have someone do the whole thing for you without any effort on your part. Isn't gonna happen here. If you have a specific question about n1337's comments, ask it, and post some code or post something that shows that you don't just want the whole thing handed to you. Asking for favors and saying you are a beginner, etc., is not the way to get help on this forum. Read the rules.

#include<iostream>
using namespace std;
void prob(float,float,float,float);

void main()
{
	int dataset;
	float Pa,Pb,K,L;
	
	cout<<"Enter how many data sets you want to enter"<<endl;
	cin>>dataset;

	for(int i=0; i<dataset; i++)
	{	
		cout<<"Enter values"<<endl;
		cin>>Pa>>Pb>>K>>L;	
	}
	prob(Pa,Pb,K,L);
}

void prob(float Pa,float Pb,float K,float L)
{
	float sum=0;
	float temp,temp1=1;

	if(Pa>Pb)
	{
		temp=(Pa/100)/2;
	}
	else 
	{
		temp=(Pb/100)/2;
	}
	for(int j=0; j<K; j++)
	{
		for(int k=0; k<L; k++)
		{
			temp1=temp1*temp;	
			sum=sum+temp1;
		}	
		
	}
	sum=sum*100;
	cout<<sum;
}

Thats All I could have done up till now!! according to you n1333. But it solves the problem for Pa=100,Pb=1,K=1,L=4 out put is 93.75
it sholud print this output when L=3. Thankyou.

This:

for(int i=0; i<dataset; i++)
{
    cout<<"Enter values"<<endl;
    cin>>Pa>>Pb>>K>>L;
}

will overwrite Pa,Pb,K & L every time the for-loop loops. So you need an array or a vector to store all the datasets.

Also please read the rules and use code tags

I thought that was not the problem but another one arised!!!....Well according to the question I have I have to enter the number of data sets and the required fields. The loops worked right the way i was taking input!! but the problem was in the final output!!...can you resolve??? or anyone???

I thought that was not the problem but another one arised!!!....Well according to the question I have I have to enter the number of data sets and the required fields. The loops worked right the way i was taking input!! but the problem was in the final output!!...can you resolve??? or anyone???

What niek_e means is that suppose you wanted 2 data sets (the sample sets). When you loop the first time, you input the values, and Pa = 100, Pb = 50, K = 1, L = 1, etc (as an example). But then you loop again, and instead of retaining these values, you overwrite them with the second data set...hopefully that makes sense. In any case, that will be an issue when you want to solve multiple trials...even if you corrected your current algorithm, the program will only ever solve the last data set that is entered.

Ok I am going to try one more time to explain it without writing code...I can't do up the code for you (for one because I'm at work and two because this is a great learning experience for you...). As for your comment about the course your taking being a beginner course and not one in logic...well...logic/math/problem solving will ALWAYS be a part of programming, they go hand-in-hand, beginner course or not.

The way I thought of this problem was to construct a "tree" like data structure, with each node being the team that is serving:

round 1                                        A
                                   (Pa)  /                        \   (1-Pa)
       round 2                       A                             B
                            (Pa)  /       \ (1-Pa)       (1-Pb) /     \  (Pb)
       round 3                A           B                    A         B

(whew...looks better on paper....lol)

Ok so each labelled node is the team serving. Any node that branches to the left indicates A has won that round, and any branch to the right is a round won by B. So the above tree represents the various probabilities if A serves first in the round. That is, A has a Pa chance of winning the first game (branch to the left), and a (1-Pa) of losing the first game (branch to the right).

Let us look at the A branch (first branch to the left) in round 2. Notice that it is exactly the same pattern as round 1 (above). That is, A is serving here again, so there is a Pa chance that it will win, and a (1-Pa) chance that it will lose (this is where my recursive definition came in to play). On the other hand, the other branch has B serving. In that case, A has a (1-Pb) chance to win, whereas it has a Pb chance to lose (since B is serving). Hopefully that makes sense.

Now we can simply follow the path of the branches. That is, in any 1 branch, the probability to win a game is simply the product of all the probabilities. So take the leftmost chain...if A wins everygame, the chance is Pa*Pa. But the TOTAL probability of A winning a game is then the SUM of all the probabilities of each chain....That is, A can win the game by winning every game (Pa*Pa), but it can also win by losing the first game, then winning everygame thereafter, etc. You need to account for all these possibilities to get the total probability of winning a game.

There is a very distinct pattern. If you look carefully at the tree, and extend it, you will notice that each node follows one of 2 patterns. That is, either A is serving at the node (and there are 2 possible outcomes), or B is serving at the node (and there are 2 possible outcomes). So this is where the recursion comes into play again. We begin at the top of the tree, but then each node thereafter can be thought of as the root node of a new tree....So you only need to solve 2 possibilites, and then just continue the pattern (either by recursion or looping, I would suggest first finding the recusive definition, then converting it to loops, because I don't think there are many, if at all, C++ compilers that optimize for mutual recursion...if your interested in that, learn Scheme lol).

Anyway, that will be the pattern for finding the probability of winning a game. Also remember you will need to repeat the entire process assuming that B serves first to get the total probability...

Then you need to construct some process to solve for when there are multiple games and such...

Thats all I can say for now, need to get back to work. Goodluck, and if you need more help, hopefully somebody else can interpret what I've been saying. No time to post code...

void main()
{
	int dataset;
	cout<<"Enter how many data sets you want to enter"<<endl;
	cin>>dataset;
	float *Pa=new float[dataset];
	float *Pb=new float[dataset];
	float *K=new float[dataset];
	float *L=new float[dataset];
	
	

	for(int i=0; i<dataset; i++)
	{	
		cout<<"Enter values"<<endl;
		cin>>*(Pa+i)>>*(Pb+i)>>*(K+i)>>*(L+i);	
	}
	prob(*Pa,*Pb,*K,*L);
}

Is that the right input?? or does it creats any problems!!!

void prob(float Pa,float Pb,float K,float L)
{
	float sum=0; 
	float temp=1;
	if(Pa>Pb)
	{
		for(int i=0; i<K; i++)
		{
			for(int j=0; j<L; j++)
			{	
				temp=temp*(Pa/100)/2;				
				sum=sum+temp;				
			}
			cout<<sum*100;
		}
	}

}

does that implement the 'A' part. As i have to calculate probability only for A. plz reply as soon as (anyone of you) can, im running short of time.

Is there anyone who could guide me???

You are giving me the bad impression!!!....can any one help!!! this is nto a solved thread!!!....no replies since two days...Plz help I've asked question...somebody plz...

What precisely is the question? What is the "probability of A"? I've reread your post 1 and I don't see what that means. You're going to have to be far more precise. I assume that "probability of A" means the "probability theat team A wins the match", but you never actually say that, as far as I can tell. If K and L are the number of rounds and games, they should be integers, not floats. You need to stick some comments in there to denote exactly what each variable means. In particular, what does the prob function do?

You'll do well to break this program up into functions like:

PlayRound ()
PlayGame ()
PlayMatch ()

I've left the parameter list and return types empty. You'll want to change them. One question that only you can answer is this: are you calculating an exact probability or are simulating these matches over and over and deriving a probability estimate from those results? How you code will depend on those answers.

#include<iostream>
using namespace std;

void prob(float,float,int,int);		

void main()
{
	int dataset;
	cout<<"Enter how many data sets you want to enter"<<endl;
	cin>>dataset;
	float *Pa=new float[dataset];	//Pa probability of A to win the match
	float *Pb=new float[dataset];	//Pb probability of B to win the match
	int K,L;							//now they are not float
	
	for(int i=0; i<dataset; i++)	//loop to input dataset and service chances
	{	
		cout<<"Enter values"<<endl;
		cin>>*(Pa+i)>>*(Pb+i)>>K>>L;//when dataset more than two it wont overwrite the values	

	}
	prob(*Pa,*Pb,K,L);			//function call
}
void prob(float Pa,float Pb,int K,int L)
{
	float sum=0; 
	float temp=1;

	if(Pa>Pb)	//if Team A services first
	{									
		for(int i=0; i<K; i++)	//outerloop for number of games
		{	
			for(int j=0; j<L+1; j++)//inner loop for number of rounds
			{
			temp=temp*((Pa/100)/2); //if A serves first it  has half chances of winning game so if Pa is 100% it would be 0.5 in this case we add 0.5 while moving through each loop. Thats the place where i need help, because it don't involves team B, which is probably wrong ofcource.
			sum=sum+temp; //ofcource we add result round by round
			}
			sum=sum*100;
			cout<<sum<<endl;
		}
	}
		
	if(Pb>Pa)					//if Team B services first
	{
		for(int i=0; i<K; i++)		//outerloop for number of games
		{
			for(int j=0; j<L; j++)	//inner loop for number of rounds
			{
			temp=temp*((Pb/100)/2);//if B serves first it has half chances of winning game so if Pb is 100% it would be 0.5 in this case we add 0.5 while moving through each loop. Thats the place where i need help, because it don't involves team A, which is probably wrong ofcource.			
				sum=sum+temp;				
			}
			sum=sum*100; //multiply by 100 to convert result into %
			cout<<sum<<endl;
		}
	}


}

One Person did that for Me but the Logic the problem was he was doing the general case and was using the input Pa=100, Pb=100, K=1 and L=3 his output was 93.75%..

As per my question, input should be Pa=100,Pb=50,K=1,L=3 output should be 93.8%

That was the big confusin. Im posting his post down here.

Ok here we go: A has a 0.5 chance of serving first. If A serves, it has a Pa chance of winning. In this case, Pa = 1, so if A ever gets to serve, it will always win. Thus we have A will win 0.5 in this case (because there is a 0.5 chance that A will serve first).

If B serves first, then at each successive round, A has a 0.5 chance of getting to serve (B has a 0.5 chance of winning a round, and therefore of losing a round as well). Thus, there is a 0.5 chance that A will win the first round if B serves first, so then if A wins that round, it will win all of its next rounds, so that gives 0.25 chance of winning. (0.5 that B serves first multiplied by 0.5 that A wins the round).

If B wins the first, then A again has a 0.5 chance of winning the second round, and therefore has a 0.5*0.5*0.5 = 0.125 chance of winning if B wins the first round.

Using the same logic, A has a 0.5*0.5*0.5*0.5 = 0.0625 of winning if B wins the first 2 rounds.

If we add all this mess up, we get 0.5 + 0.25 + 0.125 + 0.0625 = 93.75 (or 93.8).

So now what you have to do, is at each step you need to generalize the case. That is, A will not always have a 100% chance of winning, it has a Pa% chance. That means that B could win some as well, so now you will have extra branches.

Basically what I would do to think of this is keep track of the number of rounds/games won by team A and B. It might be useful to think of this problem recursively (I've been doing a lot of Scheme programming, so recursion is now my friend ) -- of course, in C or C++, there is no optimization for recursion, you would have to write tail recursive functions, which is annoying in C/C++, so just think of the problem recursively and convert it to loops (thats what I would do anyway).

At each step you basically have two cases: team A win, or team B wins. There is also a probability associated with each step. So you must just define a function such that the probability is multiplied at each step, and you just keep following that process until you reach the desired number of rounds (L), and games (K)...

Thank you if you could help me or show me some correct codes

#include<iostream>
using namespace std;

void prob(float,float,int,int);		

void main()
{
	int dataset;
	cout<<"Enter how many data sets you want to enter"<<endl;
	cin>>dataset;
	float *Pa=new float[dataset];	//Pa probability of A to win the match
	float *Pb=new float[dataset];	//Pb probability of B to win the match
	int K,L;							//now they are not float
	
	for(int i=0; i<dataset; i++)	//loop to input dataset and service chances
	{	
		cout<<"Enter values"<<endl;
		cin>>*(Pa+i)>>*(Pb+i)>>K>>L;//when dataset more than two it wont overwrite the values	

	}
	prob(*Pa,*Pb,K,L);			//function call
}
void prob(float Pa,float Pb,int K,int L)
{
	float sum=0; 
	float temp=1;

	if(Pa>Pb)	//if Team A services first
	{									
		for(int i=0; i<K; i++)	//outerloop for number of games
		{	
			for(int j=0; j<L+1; j++)//inner loop for number of rounds
			{
			temp=temp*((Pa/100)/2); //if A serves first it  has half chances of winning game so if Pa is 100% it would be 0.5 in this case we add 0.5 while moving through each loop. Thats the place where i need help, because it don't involves team B, which is probably wrong ofcource.
			sum=sum+temp; //ofcource we add result round by round
			}
			sum=sum*100;
			cout<<sum<<endl;
		}
	}
		
	if(Pb>Pa)					//if Team B services first
	{
		for(int i=0; i<K; i++)		//outerloop for number of games
		{
			for(int j=0; j<L; j++)	//inner loop for number of rounds
			{
			temp=temp*((Pb/100)/2);//if B serves first it has half chances of winning game so if Pb is 100% it would be 0.5 in this case we add 0.5 while moving through each loop. Thats the place where i need help, because it don't involves team A, which is probably wrong ofcource.			
				sum=sum+temp;				
			}
			sum=sum*100; //multiply by 100 to convert result into %
			cout<<sum<<endl;
		}
	}


}

Lines 11 and 12 - Pa and Pb are not the probability that A or B wins the match. Reread your first post.

Line 21 - You are only calling the prob function once.

Lines 28, 42 - Pb and Pa don't tell you anything about who is serving first. See point 9 in your original post.

Lines 30, 32, 44, 46 - These loop control variables are not correct. K and L are the number of games/rounds that must be WON, not the number that must be played.

Line 34 - This comment suggests to me that you are very confused about what each variable represents. where does the 0.5 come from?

You need to explicitly define what these variables mean in comments so that both you and the reader know what a variable represents, including temp and sum. Have you written this out on paper? Do you understand the probability calculations required for this assignment? Don't try to code it until you do.

One Person did that for Me but the Logic the problem was he was doing the general case and was using the input Pa=100, Pb=100, K=1 and L=3 his output was 93.75%..

As per my question, input should be Pa=100,Pb=50,K=1,L=3 output should be 93.8%

Actually, I was not doing the general case, but only the first given case. I basically handed you the general case in my next post (where I constructed a tree diagram). And I did in fact use Pb = 50, NOT Pb = 100, in my first post. And my output of 93.75 is correct, notice that the given answer of 93.8 is simply 93.75 rounded to 1 decimal place.

And I apologize for not continuing to help, but I've done all the thinking I can for you...The only way I could make it any clearer is to write the code for you (and believe me, I don't have the time to do so), which is against the rules, and isn't really a learning experience for you. Goodluck though...

commented: well said +5

I really appriciate you(n1337) for taking out your time...Thankyou very much...you tried to help me alot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.