Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You will write a program using functions that performs the following tasks. These tasks are explained in more detail later in this document (1)

 

 

image text in transcribed

image text in transcribed

image text in transcribed

 

You will write a program using functions that performs the following tasks. These tasks are explained in more detail later in this document (1) For a user specified number of iterations, determine an approximation for pi using the Monte Carlo Method. Set the output precision to 6 decimal places for this part of the program. (2) Determine the outcome of a fair coin being flipped. For a user specified number of coin flips, determine the percent of the time that heads comes up and the percent of the time tails comes up. Set the output precision to 4 decimal places for this part. (3) Determine the outcome of a fair* 4-sided die being rolled. For a user specified number of rolls, determine the percent of the time that each side (sides 1,2,3 and 4) come up. Set the output precision to 4 decimal places for this part. *fair - means that each possible outcome is equally likely - there is no bias for one outcome over the others. For the coin it means that heads has a 50% chance of coming up and so does tails. In order to perform the above tasks, a random number generator is to be used to determine values for testing. Information regarding the random number generator occurs later in this document. To perform the above tasks, your program should have the following order (pseudo algorithm/functional decomposition setup) as illustrated in the sample solution: Prompt the user for a seed value for the random number generator Write out a menu with the choices as shown in the sample solution and obtain the selection value While the choice is not to exit, perform the following loop For choice 1, generate random numbers and process to determine the approximation for pi For choice 2, generate random numbers and process to determine the fair coin probabilities For choice 3, generate random numbers and process to determine the fair die probabilities Write out a menu for the next choice and obtain the selection value The seed value entered for the random number generator is a valid positive integer value and the number of iterations entered is a valid positive integer. Largest integer value allowed is 2147483647 This program uses the random number generator in C++. In order to compare results from the run of one program to another program, the same random numbers need to be generated by both programs. This is accomplished by supplying a seed value to start the random number generator at the same point. The code below performs the task of initializing the random number generator with a seed value: // Setup the random number generator starting point by obtaining a seed cout < < "enter in the seed (integer > 0) for the random number generator: "; cin>> seed; Open 3 of for the rand cout < < seed < < endl; // echo print out the value entered srand(seed); // use the seed entered to initialize the generator Random numbers are generated by calling the value returning function rand(). rand() returns an integer value between 0 and RAND MAX (a constant with a value of 32767). In this program, all retum values from rand() are converted to a floating-point number between 0 and 1 by dividing the returned value by RAND_MAX. An example of the type of line of code you will need to use is the following: x_coord double (rand())/double (RAND_MAX); // value is 0 to 1 Note the type casting to double on all of the integer values. For this project, at least five functions will be written. The five required functions are: 1) Print Menu - This function is to print out a menu of options only. It is to do no other task. It should be a void function. It will not read in the selection 2) Obtain Integer - This function obtains any integer value. It handles all of the error correction and messages if any characters other than digits are entered. It can be void or value returning. 3) Calculate PI - this function calculates an approximation to Pl using the monte-carlo method. This is a void function and it does not have any parameters 4) Flipping a Coin - This function determines the odds of heads or tails coming up when flipping a fair coin. This is a void function without parameters 5) Toss a Die-This void function without parameters, determines the odds of a 1, 2, 3 or 4 coming up when a four sided fair die is tossed. Monte Carlo Method: The Monte Carlo method is a way to use random numbers to approximate a value. In this program it is used to determine a value for pi. The background on the method is now discussed. Consider the figure below showing a circle of radius 1 inscribed inside a box of side length 2. The area of the square is 2*2 or 4 units. The area of the circle is PI radius radius which is PI since the radius is 1. Therefore the area of the circle divided by the area of the box is PI/4. In the Monte Carlo method, two random numbers are used to represent an X-coordinate value (from 0 to 1) and a Y-coordinate value (from 0 to 1). The position of the X-Y coordinate pair relative to the circle is determined (is the pair inside the circle or outside the circle). Page 2 of 3 Spring 2019 CPE211 Project Assignment Project 8 Two counters are used. One counter for counting the number of co-ordinate pairs that fall inside ( x+y=1.0) the cirde, and one for the number of pairs that fall outside the circle Then the ratio of the number of co-ordinate pairs inside the circle to the total number of co-ordinate pairs tested approaches the ratio of Pl/4. The more iterations performed, the closer the approximation becomes. For each loop iteration, an X and a Y co-ordinate pair is generated. The location of the X-Y pair relative to the circle is then determined. Once all loop iterations have completed, the approximate value for Pt is calculated using the formula PI-4*(number of co-ordinate pairs inside the circle)/(total number of iterations). Where - means is approximately equal to. For the Monte Carlo method, 2 random numbers are generated for each loop iteration- one for the x value and one for the y value. C) Side is 2 Coin Flip: For simulating the flipping of a coin, a random number between 0 and 1 is generated to represent each flip of the coin. If that value is less than or equal to 0.5, then assume that the coin landed on heads for values greater than 0.5 the coin is assumed to have landed on tails... Rolling a Die: For simulating the rolling of a 4-sided die, a random number between 0 and 1 is generated to represent each roll of the die. If the random number is less than 0.25, then assume side # 1 came up. If the random number is greater than or equal to 0.25 but less than 0.5, then assume side #2 came up. If the random number is greater than or equal to 0.5 but less than 0.75 then assume side #3 came up. For a random number greater than or equal to 0.75, assume that side number 4 came up. For the coin flip and die roll parts of the program, one random number is generated for each loop iteration. The menu has 26 characters across the top and bottom lines Invalid integer and invalid character messages have 47 characters across the top and bottom For the Pl output, there are 46 characters across the top and bottom of the output section For the coin output, there are 47 characters across the top and bottom of the output section For the die output, there are 45 characters across the top and bottom of the output section The only input value that needs to be tested and verified is the menu choice. Valid integer values will be supplied for the seed number and the number of iterations All user defined functions can be called from main only. User defined functions cannot call other user defined functions. The only exception is that the function to obtain an integer can call the print menu function (note the Print Menu function cannot call any user defined functions) . Global variables are not allowed Must have at least the five functions stated on page 2. Use integer variables for all counters Use double variables for all percentage and result calculations - do not forget to type cast Use double variables for the x and y coordinates used in the Monte Carlo method All user defined functions must have their definitions below main and function prototypes must be used. The function prototypes are to be placed above main and below the using namespace std; line

Step by Step Solution

There are 3 Steps involved in it

Step: 1

To tackle this C program well break it down into separate tasks as described 1 Monte Carlo Method fo... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Fundamentals Study Guide

Authors: Dr. Sergio Pisano

1st Edition

B09K1WW84J, 979-8985115307

More Books

Students also viewed these Databases questions

Question

What are the two major components of error control?

Answered: 1 week ago

Question

What is the function of the cache in HTTP?

Answered: 1 week ago

Question

* What is the importance of soil testing in civil engineering?

Answered: 1 week ago

Question

Explain the concept of shear force and bending moment in beams.

Answered: 1 week ago

Question

What are their resources?

Answered: 1 week ago

Question

What impediments deal with customers?

Answered: 1 week ago