Question
use basic c++

3. In the game of cricket, based on his past statistics of play, a batsman has the probability of scoring runs when he faces
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#include<iostream>

#include<stdlib.h>

using namespace std;

//helper method that simulates a ball faced by batsman, returns the runs

//scored in that ball. returns -1 if the batsman is out

int faceBall(){

                //generating a value between 1 and 100, representing a percentage

                int i=(rand()%100)+1;

                //finding the result based on value of i

                if(i<=5){

                                //5% time - 6

                                return 6;

                }else if(i<=8){

                                //3% time - 5

                                return 5;

                }else if(i<=20){

                                //12% time - 4

                                return 4;

                }else if(i<=25){

                                //5% time - 3

                                return 3;

                }else if(i<=50){

                                //25% time - 2

                                return 2;

                }else if(i<=80){

                                //30% time - 1

                                return 1;

                }else if(i<=93){

                                //13% time - 0

                                return 0;

                }else{

                                //7% time - out

                                return -1;

                }

}

//method to perform the simulation

int simulate(){

                //declaring number of sixes, fours, and balls to 0

                int sixes=0, fours=0, balls=0;

                int result;

                //looping

                do{

                                //facing a ball

                                result=faceBall();

                                //incrementing number of balls

                                balls++;

                                //if result is a 6, incrementing sixes

                                if(result==6){

                                               sixes++;

                                }

                                //if result is a 4, incrementing fours

                                else if(result==4){

                                               fours++;

                                }

                                //if result is a -1, batsman is out, returning the balls

                                else if(result==-1){

                                               return balls;

                                }

                }while(sixes<5 && fours<10); //until user hits 5 sixes OR 10 fours

                return balls; //returning the balls faced

}

int main(){

                //declaring total balls to 0

                int total_balls=0;

                //simulating for 5 times

                for(int i=0;i<5;i++){

                                //adding number of balls needed to total balls

                                total_balls+=simulate();

                }

                //finding average balls needed and displaying it

                double avg=(double)total_balls/5;

                cout<<"An average of "<<avg<<" balls needed to score 5 sixes or 10 fours or get out"<<endl;

                return 0;

}

/*OUTPUT*/

An average of 15.8 balls needed to score 5 sixes or 10 fours or get out

Add a comment
Know the answer?
Add Answer to:
use basic c++ 3. In the game of cricket, based on his past statistics of play,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use basic C++ 3. A text file, superstars.txt, contains statistics on cricket players. For each player,...

    Use basic C++ 3. A text file, superstars.txt, contains statistics on cricket players. For each player, the file contains the player's first name, last name, number of matches played, total number of runs scored, number of times the player scored 100 runs in a match, and the number of wickets taken. The last row of data in the file contains the word "END" only. Some rows are shown below 30 11867 164 Chanderpaul Shivnarine 34 11912 130 Lara Brian 73...

  • C# Code: Write the code that simulates the gambling game of craps. To play the game,...

    C# Code: Write the code that simulates the gambling game of craps. To play the game, a player rolls a pair of dice (2 die). After the dice come to rest, the sum of the faces of the 2 die is calculated. If the sum is 7 or 11 on the first throw, the player wins and the game is over. If the sum is 2, 3, or 12 on the first throw, the player loses and the game is...

  • 3. In this question we simulate the rolling of a die. To do this we use...

    3. In this question we simulate the rolling of a die. To do this we use the func- tion runif (1),which returns a randomnumber in the range (0,1). To get a random integer in the range (1,2,3,4,5,6), we use ceiling(6 runif (1)) or if you prefer, sarple(1:6,size-1) will do the same job. (a). Suppose that you are playing the gambling game of the Chevalier de Méré. That is, you are betting that you get at least one six in 4...

  • 3. In this question we simulate the rolling of a die. To do this we use...

    3. In this question we simulate the rolling of a die. To do this we use the func- tion runif (1),which returns a randomnumber in the range (0,1). To get a random integer in the range (1,2,3,4,5,6), we use ceiling(6 runif (1)) or if you prefer, sarple(1:6,size-1) will do the same job. (a). Suppose that you are playing the gambling game of the Chevalier de Méré. That is, you are betting that you get at least one six in 4...

  • In C... Write a program to simulate a pick-5 lottery game. Your program must generate and...

    In C... Write a program to simulate a pick-5 lottery game. Your program must generate and store 5 distinct random numbers between 1 and 9 (inclusive) in an array. The program prompts the user for: an integer random seed five distinct integers between 1 and 9 (which are stored in another array) The program then compares the two arrays to determine if they are identical. If the two arrays are identical, then the user wins the game. otherwise the program...

  • a to d Problem 6 10 points Consider the following simple lottery game. There are ten...

    a to d Problem 6 10 points Consider the following simple lottery game. There are ten balls total of which five are red and numbered 1,2,3,4,5. The remaining five are blue and also numbered 1,2,3,4,5. Three balls are simultaneously pulled out from a hat, and depending on what they are, you might win a prize. (a) You win a teddy bear if all three balls are the same color. What is the probability of winning a teddy bear? (b) You...

  • Problem 6 10 points Consider the following simple lottery game. There are ten balls total of...

    Problem 6 10 points Consider the following simple lottery game. There are ten balls total of which five are red and numbered 1,2,3,4,5. The remaining five are blue and also numbered 1,2,3,4,5. Three balls are simultaneously pulled out from a hat, and depending on what they are, you might win a prize. () You win a teddy bear if all three balls are the same color. What is the probability of winning a teddy bear? (b) You win a baby...

  • NEED LAST 2 Two individuals, A and B, are finalists for a chess championship. They will...

    NEED LAST 2 Two individuals, A and B, are finalists for a chess championship. They will play a sequence of games, each of which can result in a win for A, a win for B, or a draw. Suppose that the outcomes of successive games are independent, with P(A wins game) = 0.3, P(B wins game) = 0.2, and P(draw) = 0.5. Each time a player wins a game, he earns 1 point and his opponent earns no points. The...

  • Write a C# program that allows one user to play Rock-Paper-Scissors with computer. The user will...

    Write a C# program that allows one user to play Rock-Paper-Scissors with computer. The user will pick a move (Rock Paper Scissors) from 3 radio buttons and click the play button to play the game. The application will use random number generator to pick a move for the computer. Then, the application display the computer's move and also display whether you won or lost the game, or the game is tie if both moves are the same. The application must...

  • Assignment Specifications We are going to use the Monte Carlo Method to determine the probability of...

    Assignment Specifications We are going to use the Monte Carlo Method to determine the probability of scoring outcomes of a single turn in a game called Pig. Your Task For this assignment you will simulate a given number of hold?at?N turns of a game called Pig, and report the estimated probabilities of the possible scoring outcomes. You are NOT implementing the game of Pig, only a single turn of this game. The value of N will be acquired via user...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT