#include "stdstuff.h" #include "odds.h" // Here's a solution to the "food for thought" part of the assignment. int _count = 0; double probabilityOfState (int AWins, int GWins) { double result; if (AWins+GWins>7) { result = 0.0; return result; } if ((AWins == 0) && (GWins == 0)) { result = 1.0; _count++; // count every time we get to here } else if ((AWins == 0) || (GWins == 4)) { result = probabilityOfState (AWins, GWins - 1) * (1 - probabilityOfAngelVictory (AWins, GWins - 1)); } else if ((GWins == 0) || (AWins == 4)) { result = probabilityOfState (AWins - 1, GWins) * probabilityOfAngelVictory (AWins - 1, GWins); } else { result = ( probabilityOfState (AWins, GWins - 1) * (1 - probabilityOfAngelVictory (AWins, GWins - 1)) ) + ( probabilityOfState (AWins - 1, GWins) * probabilityOfAngelVictory (AWins - 1, GWins) ); } return result; } void realMain () { int i, j; double P, AngelsP = 0, GiantsP = 0; cout << setiosflags (ios::fixed | ios::showpoint) << setprecision(6); // Let's create a new table where probOFIJ[i][j] represents the probability that at // some point in the series, the Angels have won i games and the Giants have won j. // Note that we calculate this using probabilityOfState. double probOfIJ [5][5]; for (i=0; i<=4; i++) { for (j=0; j<=4; j++) { probOfIJ[i][j]=probabilityOfState(i,j); } } // Let's now print out the table, so we can have a look at it. cout << "\nHere's the new probOfIJ table (i, j, and value):\n"; for (i=0; i<=4; i++) { for (j=0; j<=4; j++) { cout << "\n" << i << " "<< j << " " << probOfIJ[i][j]; } } cout << "\n\nHere are the old calculations:\n"; for (i = 0; i <= 3; i++) { P = probabilityOfState (4, i); cout << "The probability of the Angels winning in " << (4 + i) << " games is " << P << ".\n"; AngelsP += P; P = probabilityOfState (i, 4); cout << "The probability of the Giants winning in " << (4 + i) << " games is " << P << ".\n"; GiantsP += P; } cout << "\nThe probability of the Angels winning the series is " << AngelsP << ".\n"; cout << "The probability of the Giants winning the series is " << GiantsP << ".\n"; cout << "\nThe sum of all of the probabilities is " << (AngelsP + GiantsP) << ".\n"; cout << "\nNow let's see how many times probabilityOfAngelVictory is called "; cout << "\nwhen calculating the probability of the Angels winning in 7 games:\n"; _count = 0; P = probabilityOfState(4,3); cout << "\nThe probability of the Angels winning in 7 games is " << P << ".\n"; cout << "The number of times the recursive function was called for (0,0) is " << _count << ".\n"; cout << "\nNow let's do the calculations using probOfIJ instead:\n"; AngelsP = GiantsP = 0; for (i = 0; i <= 3; i++) { P = probOfIJ[4][i]; cout << "The probability of the Angels winning in " << (4 + i) << " games is " << P << ".\n"; AngelsP += P; P = probOfIJ[i][4]; cout << "The probability of the Giants winning in " << (4 + i) << " games is " << P << ".\n"; GiantsP += P; } cout << "\nThe probability of the Angels winning the series is " << AngelsP << ".\n"; cout << "The probability of the Giants winning the series is " << GiantsP << ".\n"; cout << "\nThe sum of all of the probabilities is " << (AngelsP + GiantsP) << ".\n"; P = probOfIJ[4][3]; cout << "\nThe probability of the Angels winning in 7 games is " << P << ".\n"; cout << "Note that we don't have any recursive calls this time!\n\n"; } int main () { try { realMain (); } catch (exception &e) { // catches all uncaught exceptions cout << "\nException <" << e.what() << "> occurred.\n"; } pause (); return 0; }