Click here to Skip to main content
15,881,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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; //probabilities of each team after 
    scoring fieldgoal or touchdown or no score
    float p1a,p2a,p1b,p2b; //probabilities of each team for success of 
    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)//alternate chance for teams
            {
                randomIndex= rand() % 3;
                randomValue= myArray[randomIndex];
                if(randomValue==0) //if no score
                {
                    k+=randomValue;
                    d+=randomValue;
                    rva0++;
                }
                else if(randomValue==3) //if field goal
                {
                    k+=randomValue;
                    d+=randomValue; rva3++;
                }
                else //if touchdown
                {
                    randomextra=rand() % 2 + 1;
                    if(randomextra==1){ //if 1 point
                    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{ //if 2 point
                        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 //chance of team B
            {
                randomIndex= rand() % 3;
                randomValue= myArray[randomIndex];
                if(randomValue==0) //if no score
                {
                    k+=randomValue;
                    d-=randomValue; rvb0++;
                }
                else if(randomValue==3) //if field goal
                {
                    k+=randomValue;
                    d-=randomValue; rvb3++;
                }
                else //if touchdown
                {
                    randomextra=rand() % 2 + 1;
                    if(randomextra==1){ //if 1 point
                    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{ //if 2 point
                        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);

    }
Posted
Updated 25-Jan-18 2:58am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900