Mighty Casey plays centerfield for the Toledo Mudhens and has the following life
ID: 3733534 • Letter: M
Question
Mighty Casey plays centerfield for the Toledo Mudhens and has the following lifetime hitting percentages Out Walk Single Double Triple Home Run 5% 35% 16% 20% 15% The above is called a probability distribution. A probability distribution is used in predicting the likelihood of an event occurring. See the following explanation of probability distributions using the example of flipping a coin at: http://stattrek.com Lesson 2 ProbabilityDistribution. aspx. Notice the distribution adds up to 100% Write a program to simulate a large number of times at bat (1000 or more) for Mighty Casey counting the number of outs, walks, singles, etc. to predict Mighty Casey's batting average for next season and slugging percentage. This means you have to generate a random number (hint: between 1 and 100) and based upon the value (i.e. probability) will determine if Mighty Casey gets a hit or an out. If it is a hit, then your program needs to determine what type of hit was based upon the probabilities given in the table above Use the formulas below to calculate the batting average and slugging percentage of Mighty Casey batting averagenumber of hits / (number of times at bat number of walks) slugging percentage = (number of singles + number of doubles * 2 + number of triples * 3 + number of homeruns * 4) /(number of times at bat - number of walks)Explanation / Answer
//C++ program to find the batting average and slugging percentage
//main.cpp
//include header files
#include<iostream>
#include<iomanip>
#include<stdlib.h>
using namespace std;
//set NUM_BATS =1000
#define NUM_BATS 1000
int main()
{
//intialize variabels to zero
int at_bat=0;
int out=0;
int walk=0;
int single=0;
int doubles=0;
int triple=0;
int home_run=0;
int run=0;
double batting_avg=0;
double slugging_percentage=0;
srand(time_t(0));
//run for loop
for(int i=0;i<NUM_BATS;i++)
{
run=rand()%100+1;
switch(run)
{
case 35:
out++;
break;
case 16:
at_bat++;
walk++;
break;
case 20:
at_bat++;
single++;
break;
case 15:
at_bat++;
doubles++;
break;
case 9:
at_bat++;
triple++;
break;
case 5:
at_bat++;
home_run++;
break;
}
}
//calculate batting average
batting_avg=NUM_BATS/(at_bat-walk);
//calculate slugging percentage
slugging_percentage=(single+2*doubles+3*triple+4*home_run)/(at_bat-walk);
cout<<fixed<<setprecision(2);
cout<<"Batting average : "<<batting_avg<<endl;
cout<<"Slugging Percentage : "<<slugging_percentage<<endl;
system("pause");
return 0;
}
Sample Output :
Batting average : 21.00
Slugging Percentage : 2.00