In C++ . Do not use arrays. Out Walk 9.7% Single 22.0% Double 6.1% Triple 2.5% H
ID: 3906223 • Letter: I
Question
In C++ . Do not use arrays. Out Walk 9.7% Single 22.0% Double 6.1% Triple 2.5% Home Run 1.7% 58.0% Based on the above percentages, write a program to simulate Casey stepping up to the plate 1000 times and count and display the number of outs, walks, singles and so on. Then calculate and display his batting average: number of hits batting average- Times at bat - number of walks Hits singles doubles +triples + home runs Enclose your logic in a do while which will ask the user if he or she wishes to continue and accepts a char value of Y or N as an answer. Run and capture execution samples of at least two simulations.Explanation / Answer
though I wasn't very clear about the question.. as far as simulation is concerned the below code might help you.
#include <iostream>
#include<string>
using namespace std;
int main()
{
//variables for different parameters
int Hits;
int outs;
int walks;
int singles;
int Doubles;
int triples;
int homeruns;
double batavg;
char c;//variable to check if loop continues
do{
for(int i=0;i<1000;i++)//1000 turns of casey
{
//sampling according to the percentages given
if(rand()%100<58)
outs+=1;
else if(rand()%100<22.0)
singles+=1;
else if(rand()%100<9.7)
walks+=1;
else if(rand()%100<6.1)
Doubles+=1;
else if(rand()%100<2.5)
triples+=1;
else if(rand()%100<1.7)
homeruns+=1;
}
Hits=singles+Doubles+triples+homeruns; //calculation of hits
batavg=Hits/(1000-walks); //batting avg
//printing the details
cout<<"no. of singles:"<<singles<<endl;
cout<<"no. of doubles:"<<Doubles<<endl;
cout<<"no. of triples:"<<triples<<endl;
cout<<"no. of homeruns:"<<homeruns<<endl;
cout<<"no. of outs:"<<outs<<endl;
cout<<"another try? (Y/N):"<<endl;
cin>>c;
}while(c!='N');
return 0;
}
<