#include "stdstuff.h" #include "odds.h" double probabilityOfState (int AWins, int GWins) { double result; if ((AWins == 0) && (GWins == 0)) { // we always start at 0 0 result = 1.0; } else if ((AWins == 0) || (GWins == 4)) { // only Giants win result = probabilityOfState (AWins, GWins - 1) * (1 - probabilityOfAngelVictory (AWins, GWins - 1)); } else if ((GWins == 0) || (AWins == 4)) { // only Angels win result = probabilityOfState (AWins - 1, GWins) * probabilityOfAngelVictory (AWins - 1, GWins); } else { // a mix of Angels and Giants wins result = ( probabilityOfState (AWins, GWins - 1) * (1 - probabilityOfAngelVictory (AWins, GWins - 1)) ) + ( probabilityOfState (AWins - 1, GWins) * probabilityOfAngelVictory (AWins - 1, GWins) ); } return result; } void realMain () { int i; double P, AngelsP = 0, GiantsP = 0; cout << setiosflags (ios::fixed | ios::showpoint) << setprecision(6); for (i = 0; i <= 3; i++) { // calculate all probabilities (4,0), (0,4), (4,1), etc. 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"; } int main () { try { realMain (); } catch (exception &e) { // catches all uncaught exceptions cout << "\nException <" << e.what() << "> occurred.\n"; } pause (); return 0; }