C++ Version, Listing
// ------- BBallCpp.Cpp Baseball probability program in C++
char name[] = "BBallCpp - Baseball probability (if each game is a 50-50 chance).";
char version[] = "C++ Version 2.03, last revised: 1993-09-27, 0600 hour";
char author[] = "Copyright (c) 1981-1993 by author: Harry J. Smith,";
char address[] = "19628 Via Monte Dr., Saratoga CA 95070. All rights reserved.";
// Computes the probability that the 1st place team will beat the 2nd place
// team for the division title, assuming each has a 50-50 chance of winning
// any given future game. Uses a bivariate binomial distribution as a model.
// Developed in Turbo Pascal 5.0, converted to
// C++ using Borland C++ Version 3.1
#include <stdio.h> // Definitions for stream input/output
#include <math.h> // Definitions for the math floating point package
#include <conio.h> // Direct MS-DOS console input/output, getch()
#include <stdlib.h> // Definition of exit() etc.
#include <string.h> // Definition of srtcpy() etc.
#include <iostream.h> // Definition of cin, cout etc.
#include <iomanip.h> // Definition of setw, setprecision etc.
#define DEBUG
#undef DEBUG
// Global constants and variables
const char ESC = 27;
char TN1[11]; // Team's name, 1st place team
char TN2[11]; // Team's name, 2nd place team
int GL1; // Games Left to play, 1st place team
int GL2; // Games Left to play, 2nd place team
int GE; // Games to play each other
double GA; // Games 1st place team is ahead. 0, 0.5, ...
int GA2; // Twice games ahead = 2 * GA, 0, 1, ...
int MNT1; // Magic Number to tie for 1st place team
int MNW1; // Magic Number to win for 1st place team
int MNT2; // Magic Number to tie for 2nd place team
int MNW2; // Magic Number to win for 2nd place team
double P; // Probability that 1st place team beats 2nd place team
double Q; // Probability that 2nd place team beats 1st place team
char Ch;
// Procedure prototypes
void init( void); // Initialize
void GetCase( void); // Get data for a case to compute
void ExpandCase( void); // Compute related data
void DisplayCase( void);
void ComputeProb( void); // Compute probability using a bivariate binomial
// distribution as a model
void DisplayProb( void); // Display probability
void ReadInt( char *Mess, int Min, int Max, int Nom,
int *I); // Read in an integer from keyboard
void ReadReal( char *Mess, double Min, double Max, double Nom,
double *I); // Read in a Real from keyboard
// ------- main program BBallCpp
int main( void) {
do {
init();
GetCase();
ExpandCase();
ComputeProb();
init();
DisplayCase();
DisplayProb();
cout << "Press any key to continue... (or ESC to exit)\n"; Ch = getch();
} while (Ch != ESC);
return 0;
} // --end-- main
// ------- Initialize
void init( void)
{
cout << " [1;33;44m [2J" // Ansi.Sys ESC seq. for YELLOW on BLUE, clrscr
<< "\n" << name << "\n" << version << "\n"
<< author << "\n" << address << "\n\n"
<< setiosflags( ios::fixed);
} // --end-- init
// ------- Get data for a case to compute
void GetCase( void)
{
int I;
// Giants, Braves 1993
strcpy( TN1, "Braves"); strcpy( TN2, "Giants");
GL1 = 6; GL2 = 7; GE = 0; GA = 1.5; // 1993-09-27, 1 / Q = 4.7175
ReadInt("Games Left to play, 1st place team (-2 => Exit, -1 => Test case)",
-2, 162, -1, &I);
if (I == -2) exit(0); // Exit main
if (I >= 0) {
strcpy( TN1, " "); strcpy( TN2, " ");
GL1 = I;
ReadInt("Games Left to play, 2nd place team", 0, 162, GL1, &GL2);
ReadInt("Games to play each other", 0, 24, 0, &GE);
do
ReadReal("Games ahead, 0, 0.5, ...", 0, 162, 0, &GA);
while (floor( GA + GA) != GA + GA);
}
} // --end-- GetCase
// ------- Compute related data
void ExpandCase( void)
{
int I;
GA2 = 2 * GA + 0.5;
I = GL1 + GL2 - GA2;
if (I % 2 == 1) { // if I is odd
cout << "Error in data\n";
cout << "Press any key to continue...\n"; Ch = getch();
}
MNT1 = I / 2; MNW1 = MNT1 + 1;
MNT2 = GL1 + GL2 - MNT1; MNW2 = MNT2 + 1;
} // --end-- ExpandCase
// ------- Diaplay the Case
void DisplayCase( void)
{
cout << setw(8)
<< GL1 << " = Games Left to play, 1st place team (" << TN1 << ")\n"
<< setw(8)
<< GL2 << " = Games Left to play, 2nd place team (" << TN2 << ")\n"
<< setw(8) << GE << " = Games to play each other\n"
<< setprecision(1)
<< setiosflags( ios::showpoint) // force trailing zeros to be printed
<< setw(8) << GA << " = Games 1st place team is ahead. 0, 0.5, ...\n"
<< setw(8) << MNT1 << " = Magic Number to tie for 1st place team\n"
<< setw(8) << MNW1 << " = Magic Number to win for 1st place team\n"
<< setw(8) << MNT2 << " = Magic Number to tie for 2nd place team\n"
<< setw(8) << MNW2 << " = Magic Number to win for 2nd place team\n\n"
<< resetiosflags( ios::showpoint);
} // --end-- DisplayCase
// ------- Compute probability using a bivariate binomial dist. as a model
void ComputeProb( void)
{
int I, J;
double A, B;
double E[25]; // Binomial coefficients, games to play each other
double S[25]; // Running sum of 2 * E[I]
double F[163]; // B. C., games not played with each other
double G[163]; // Sum of 2 * E[I] for 2nd place team win, E[I] for tie
A = GL1 + GL2 - GE - GE; // A = not played with each other games
B = 1.0;
F[0] = 1.0;
for (I = 1; I <= MNT1; I++) { // Compute binomial coefficients
F[I] = F[I - 1] * A / B;
A = A - 1.0;
B = B + 1.0;
}
A = GE;
B = 1.0;
E[0] = 1.0;
S[0] = 2.0;
for (I = 1; I <= GE; I++) { // Compute binomial coefficients
E[I] = E[I - 1] * A / B;
A = A - 1.0;
B = B + 1.0;
S[I] = S[I - 1] + 2 * E[I];
}
for (I = 0; I <= MNT1; I++) { // Compute G[I]
J = (MNT1 - I) / 2;
if (J <= GE) {
G[I] = S[J];
if ((J + J) == (MNT1 - I)) G[I] = G[I] - E[J]; // Adjust for tie
}
else
G[I] = S[GE];
}
#ifdef DEBUG
DisplayCase();
for (I = 0; I <= MNT1; I++) {
cout << "F[" << I << "] = " << F[I] << "\n";
}
cout << "\nPress any key to continue...\n"; Ch = getch();
cout << "\n";
for (I = 0; I <= GE; I++) {
cout << "E[" << I << "] = " << E[I] << "\n";
}
cout << "\nPress any key to continue...\n"; Ch = getch();
cout << "\n";
for (I = 0; I <= GE; I++) {
cout << "S[" << I << "] = " << S[I] << "\n";
}
cout << "\nPress any key to continue...\n"; Ch = getch();
cout << "\n";
for (I = 0; I <= MNT1; I++) {
cout << "G[" << I << "] = " << G[I] << "\n";
}
cout << "\nPress any key to continue...\n"; Ch = getch();
cout << "\n";
#endif
Q = 0.0; // Compute probability that 2nd place team beats 1st place team
for (I = 0; I <= MNT1; I++) {
Q = Q + F[I] * G[I];
}
A = GL1 + GL2 - GE + 1;
B = pow( 2.0, A); // 2 ** Flips
Q = Q / B;
P = 1.0 - Q;
} // --end-- ComputeProb
// ------- Display probability
void DisplayProb( void)
{
cout << setprecision(4)
<< setiosflags( ios::showpoint) // force trailing zeros to be printed
<< setw( 10)
<< P << " = P = Probability that 1st place team beats 2nd place team\n";
cout << setw( 10)
<< Q << " = Q = Probability that 2nd place team beats 1st place team\n";
if (Q > 0.0)
cout << setw( 10)
<< 1.0 / Q << " = 1 / Q, (Odds = " << P / Q << " : 1)\n";
cout << "\n" << resetiosflags( ios::showpoint);
} // --end-- DisplayProb
// ------- Read in an integer from keyboard
void ReadInt( char *Mess, int Min, int Max, int Nom, int *I)
{
char St[129];
int Stat;
long int LI;
do {
cout << Mess << "\n"
<< " [" << Min << ", " << Max << "] (ENTER => " << Nom << "): ";
cin.getline( St, 129);
Stat = sscanf( St, "%ld", &LI);
} while (!(((Stat == 1) && (LI >= Min) && (LI <= Max)) || (St[0] == 0)));
if (St[0] == 0) LI = Nom;
*I = LI;
cout << "Input = " << *I << "\n\n";
} // --end-- ReadInt
// ------- Read in a Real from keyboard
void ReadReal( char *Mess, double Min, double Max, double Nom, double *R)
{
char St[129];
char *ior;
int Stat;
do {
cout << Mess << "\n"
<< " [" << Min << ", " << Max << "] (ENTER => " << Nom << "): ";
cin.getline( St, 129);
Stat = sscanf( St, "%lf", R);
} while (!(((Stat == 1) && (*R >= Min) && (*R <= Max)) || (St[0] == 0)));
if (St[0] == 0) *R = Nom;
cout << "Input = " << *R << "\n\n";
} // --end-- ReadReal
// --end-- file BBallCpp.Cpp