Question
Write code that computes the value of pi using the Monte Carlo Method. Your code should get the number of darts to throw from the
Write code that computes the value of pi using the Monte Carlo Method. Your code should get the number of "darts" to "throw" from the first (and only) command-line argument.
Your code should print the value of pi with 10 digits to the right of the decimal point.
Each dart will consist of two double-precision floating point numbers: an X coordinate and a Y coordinate. In order to generate the two random numbers you need for each dart, you will call the following function twice:
double drand48();
This function returns a random 64-bit floating point non-negative number whose value is in the interval [0.0 .. 1.0). Make sure to generate each dart's X coordinate first and then generate the Y coordinate.
Before you call drand48() the first time, you need to initialize the random number generator by calling:
srand48(0);
You should only call this function ONCE. The argument of 0 is required; no other value is allowed.
Both of the random number generation functions mentioned above require you to include stdlib.h.
YOU ARE REQUIRED TO USE THE RANDOM NUMBER GENERATION FUNCTIONS SPECIFIED ABOVE. THE USE OF ANY OTHER RANDOM NUMBER FUNCTION(S) WILL RESULT IN A GRADE OF 0.
To figure out whether a dart has landed within the circle, you will use the Pythagorean Theorem, d^2 == x^2 + y^2. ("^2" is used to denote "squared" in this formula. The symbol 'd' represents the distance of the "dart" from the center of the circle.)
Count darts whose 'd' value is <= 1 as being INSIDE the circle. Count darts whose 'd' value is > 1 as OUTSIDE the circle.
You will need to do the Pythagorean Theorem floating point calculations by writing a small amount of GAS assembler code. In this code, you are required to use only the syntax demonstrated in course file objectCode/asm.c. You are not allowed to use any other GAS syntax, such as %0, or any specifier other than "=m" in an "output" or "m" in an "input."
THE USE OF ANY OTHER SYNTAX IN YOUR ASSEMBLY CODE WILL RESULT IN A GRADE OF 0.
You need to use assembly language to compute the value of 'd'. You do not need to use assembly language to determine whether the "dart" is within the circle. You can use an "if" statement for that.
You need to use assembly language to compute the value of 'd'. You do not need to use assembly language to determine whether the "dart" is within the circle. You can use an "if" statement for that.
Your code will get the number of darts to throw from the command line. Your code will be called like this to throw 10 darts:
a02 10
Your code will be tested by using the a02test bash script. Each run of your code should produce output as shown in a02outputCorrect.txt. Your code will be graded by asking it to throw darts according to the following series of numbers: 10, 100, 1000, 10000, 100000, 1000000. For each of these values, it will be required to produce numbers identical to what's shown in
a02correctValues.txt.
Start with a02.c from this directory. Make sure to have your code print out YOUR name instead of "I. Forgot".
***************************************************** a02.c
#include
int main() { printf("CS 201 - Assignment 2 - I. Forgot ");
return 0; }
*******************************************
a02outputSample.txt
a02 10 CS 201 - Assignment 2 - I. Forgot 10 darts : pi = 3.2000000000 ***********************************************
a02correctValues.txt
10 darts : pi = 3.2000000000 100 darts : pi = 2.9600000000 1000 darts : pi = 3.1080000000 10000 darts : pi = 3.1144000000 100000 darts : pi = 3.1346000000 1000000 darts : pi = 3.1419240000 *******************************************************
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started