PLEASE DO NOT COPY AND PASTE A PROGRAM FOUND ONLINE AS AN ASWER. I know I can fi
ID: 3854225 • Letter: P
Question
PLEASE DO NOT COPY AND PASTE A PROGRAM FOUND ONLINE AS AN ASWER. I know I can find C++ codes all over the internet to do this, but I am looking for someone who can help me write AND understand a low level C++ code to perform this calculation and please write coments explaining what each line of code is doing, It will help me understand how the code flows and works. Thank you!
Mighty Casey plays centerfield for the Toledo Mudhens and has the following lifetime hitting percentages:
Out
35%
Walk
16 %
Single
20%
Double
15%
Triple
9%
Home Run
5%
The above is called a probability distribution. A probability distribution is used in predicting the likelihood of an event occurring.
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 average = number 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)
Out
35%
Walk
16 %
Single
20%
Double
15%
Triple
9%
Home Run
5%
Explanation / Answer
Mighty Casey lifetime hitting percentages:
Out 35%
Walk 16%
Single 20%
Double 15%
Triple 9%
Home Run 5%
so when will use it in programming this will become percentage like
out = 35% = 35/100 = 0.35
Wal = 16% = 16/100 = 0.16
Single=20%= 20/100 = 0.20
Double= 15% = 15/100 =0.15
Triple = 9% = 9/100 =0.09
Home Run =5% = 5/100=0.05
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
so times at bat is 1000 or more.
first will create one variable timeatbat which is integer type
int timeatbat ;
which will generate random number to find out times at bat(1000 or more)
timeatbat = 1000 + rand();
rand() : is function which generate random number
srand() : Srand will seed the random number generator to create different random no every time the program is executed
will print this variable timeatbat
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.
for hit or miss we required other variable integer type
int hitOrMiss = 1 + rand() % 100; //random number generator from 1 - 100 to find probabilty of hit or miss and print the same
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.
for each we required variable out, walk, single, doubleHits, triple, homeRun, battingAverage, sluggingPercentage of datatype double
we initialize the value for each variable created now as mentioned in table
out = 0.35 * timeAtBat;
walk = 0.16 * timeAtBat;
single = 0.20 * timeAtBat;
doubleHits = 0.15 * timeAtBat;
triple = 0.09 * timeAtBat;
homeRun = .05 * timeAtBat;
print the same
then calculate remaining batting average ,slugging percentage as given formula
nut before doing this u need to find numberOfHits ,numberOfWalks each with variable
numberOfHits = single + doubleHits + triple + homeRun;
numberOfWalks = out + walk;
battingAverage = numberOfHits / (timeAtBat - numberOfWalks);
sluggingPercentage = (single + doubleHits * 2 + triple * 3 + homeRun * 4) / (timeAtBat - numberOfWalks);
print the same
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
int timeAtBat;
double out, walk, single, doubleHits, triple, homeRun, battingAverage, sluggingPercentage;
double numberOfHits, numberOfWalks;
srand(time(NULL));
timeAtBat = 1000 + rand(); //random number generator to find out times at Bat
cout << "Time at Bat: " << timeAtBat << endl;
//don't know what to do with this information
int hitOrMiss = 1 + rand() % 100; //random number generator from 1 - 100 to find probabilty of hit or miss
cout << "hit or miss: " << hitOrMiss << endl << endl;
//calculate probability of hit miss based on chart
out = 0.35 * timeAtBat;
walk = 0.16 * timeAtBat;
single = 0.20 * timeAtBat;
doubleHits = 0.15 * timeAtBat;
triple = 0.09 * timeAtBat;
homeRun = .05 * timeAtBat;
cout << "Out: " << out << endl;
cout << "Walk: " << walk << endl;
cout << "Single: " << single << endl;
cout << "Doubles: " << doubleHits << endl;
cout << "Triples: " << triple << endl;
cout << "Home Runs: " << homeRun << endl << endl;
numberOfHits = single + doubleHits + triple + homeRun;
numberOfWalks = out + walk;
battingAverage = numberOfHits / (timeAtBat - numberOfWalks);
sluggingPercentage = (single + doubleHits * 2 + triple * 3 + homeRun * 4) / (timeAtBat - numberOfWalks);
cout << "Number of Hits: " << numberOfHits << endl;
cout << "Number of Walks: " << numberOfWalks << endl;
cout << setprecision(3) << fixed << "Batting average: " << battingAverage << endl;
cout << "Slugging %: " << sluggingPercentage << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.