The above example is taken from book Algorithms Design and Applications by Michael T. GoodRich (page number 336 to 338 in text book and in pdf it's from 354 to 356, section 12.4.2)
link to the pdf
So my question is that where I've gone wrong in calculating the probability for team A to be success when its 1 point and 2 point score after touchdown because the output what I'm getting is very large where it should be 0.98 for 1 point and 0.40 to 0.55 for 2 point conversion?(explanation given in the above pdf link)
The code what I've written is given below :
What I have tried:
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
float fball(int k, int d, int n1);
int main()
{
srand(time(NULL));
int n1,k=0,d=0;
float f;
printf("\nEnter the number of possessions : \n");
scanf("%d",&n1);
f=fball(k,d,n1);
return 0;
}
float fball(int k, int d, int n1)
{
int n=0,i,randomextra;
int rva0,rva3,rva6,rvb0,rvb3,rvb6;
scoring fieldgoal or touchdown or no score
float p1a,p2a,p1b,p2b;
scoring 1 or points after a touchdown
int myArray[3] = {0,3,6};
int randomIndex;
int randomValue;
for(n=n1;n>0;n--)
{
if(n%2==0)
{
randomIndex= rand() % 3;
randomValue= myArray[randomIndex];
if(randomValue==0)
{
k+=randomValue;
d+=randomValue;
rva0++;
}
else if(randomValue==3)
{
k+=randomValue;
d+=randomValue; rva3++;
}
else
{
randomextra=rand() % 2 + 1;
if(randomextra==1){
p1a=p1a*(((rvb6/100)*fball(6,d-5,n-1))+((rvb3/100)*fball(3,d-2,n-1))+((rvb0/100)*fball(0,d+1,n-1)))+(1-p1a)*(((rvb6/100)*fball(6,d-6,n-1))+((rvb3/100)*fball(3,d-3,n-1))+((rvb0/100)*fball(0,d,n-1)));
}
else{
p2a=p2a*(((rvb6/100)*fball(6,d-5,n-1))+((rvb3/100)*fball(3,d-2,n-1))+((rvb0/100)*fball(0,d+1,n-1)))+(1-p2a)*(((rvb6/100)*fball(6,d-6,n-1))+((rvb3/100)*fball(3,d-3,n-1))+((rvb0/100)*fball(0,d,n-1)));
}
rva6++;
}
}
else
{
randomIndex= rand() % 3;
randomValue= myArray[randomIndex];
if(randomValue==0)
{
k+=randomValue;
d-=randomValue; rvb0++;
}
else if(randomValue==3)
{
k+=randomValue;
d-=randomValue; rvb3++;
}
else
{
randomextra=rand() % 2 + 1;
if(randomextra==1){
p1b=p1b*(((rva6/100)*fball(6,d-5,n-1))+((rva3/100)*fball(3,d-2,n-1))+((rva0/100)*fball(0,d+1,n-1)))+(1-p1b)*(((rva6/100)*fball(6,d-6,n-1))+((rva3/100)*fball(3,d-3,n-1))+((rva0/100)*fball(0,d,n-1)));
}
else{
p2b=p2b*(((rva6/100)*fball(6,d-4,n-1))+((rva3/100)*fball(3,d-1,n-1))+((rva0/100)*fball(0,d+2,n-1)))+(1-p1b)*(((rva6/100)*fball(6,d-6,n-1))+((rva3/100)*fball(3,d-3,n-1))+((rva0/100)*fball(0,d,n-1)));
}
rvb6++;
}
}
}
printf("\np1a=%f p2a=%f p1b=%f p2b=%f\n",p1a,p2a,p1b,p2b);
}