Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IMPORTANT!!!! DO NOT COPY AND PASTE THIS CODE!!!!!!!!!!! #include #include #include #include #define SEED 35791246 int main(int argc,char* argv[]){ int iter=0; double x,y; int i,count=0;

IMPORTANT!!!!

DO NOT COPY AND PASTE THIS CODE!!!!!!!!!!!

#include #include #include #include #define SEED 35791246

int main(int argc,char* argv[]){

int iter=0; double x,y; int i,count=0; double z; double pi;

printf("Enter the number of iterations used to estimate pi: "); scanf("%d",&iter);

srand(SEED); count=0;

for(i=0;i

x=(double)drand48()/RAND_MAX; y=(double)drand48()/RAND_MAX; z=x*x+y*y;

if(z<=1) count++;

pi=(double)count/iter*4; printf("/n# of trials=%d,estimate of pi is %g ",iter,pi); } }

^^^^^^^^^ THAT CODE IS NOT IN SCOPE OF THE QUESTION. DO NOT USE IT AS AN ANSWER!!!!

If you look around on here you'll notice that every time that has been given as the answer it gets voted down multiple times.

----------------------------------------------------------------------------------------------------------------------------

Please look at the following sections: 1. The Question 2. Attachment of asm.c (mentend in the question). 3. Attachment of started code. 4. Example output. 5. Correct values of output.

----------------------------------------------------------------------------------------------------------------------------

1. The following is the question:

Write code in C 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. YOU CAN NOT SET A SEED!!!!!!!!

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 asm.c (attached below). 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."

YOU CANNOT USE ANY OTHER SYNTAX IN YOUR ASSEMBLY CODE.

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: p03 10 (See starter code and example output)

----------------------------------------------------------------------------------------------------------------------------

2. The following is asm.c:

// asm.c (see question above)

char charArray[] = {'a', 'b', 'c', 'd', 'e'}; int intArray[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};

int main(int argc, char **argv) { int localVar = 0x12345678; double fpVar = 16.0; // embedded assembly language asm( "mov $0x89ABCDEF, %%eax " "mov $0xFF, %%bl " "movsx %%bl, %%eax " "movzx %%bl, %%eax "

// two instructions in one asm statement // (not a good idea, but interesting that you can do this) "mov $0x12, %%al mov $0x34, %%ah "

// swapping two values "mov $0xBBBBBBBB, %%ebx " "mov $0xAAAAAAAA, %%eax " "xchg %%ebx, %%eax "

// extended asm "mov %[localVarIn], %%eax " // store localVar into eax "add $4, %%eax " // add 4 to eax "mov %%eax, %[localVarOut] " // copy register to localVar

// int array "mov $3, %%ecx " // initialize ecx "lea %[intArray], %%ebx " // load address of start of array "mov (%%ebx), %%eax " // get first element of array "mov (%%ebx, %%ecx, 4), %%eax " // get indexed element of array

// char array "mov $0, %%eax " // clear eax "lea %[charArray], %%ebx " // load address of start of array "mov (%%ebx), %%al " // get first element of array "mov (%%ebx, %%ecx, 1), %%al " // get indexed element of array

// conditional statement "cmp $3, %[localVarIn] " // comparison operation "jg gt " // conditional jump if > 3 "nop " // didn't jump "gt: nop " // conditional jump target

// floating point load, store "fldl %[fpVarIn] " // load fpVar onto floating point stack "fld %%st " // duplicate value on top of fp stack "faddp " // add the two values on the fp stack "fstpl %[fpVarOut] " // store the sum back into fpVar

: [localVarOut] "=m" (localVar), // outputs [fpVarOut] "=m" (fpVar) : [localVarIn] "m" (localVar), // inputs [intArray] "m" (intArray), [charArray] "m" (charArray), [fpVarIn] "m" (fpVar) : "eax", "ebx", "ecx" // clobbers );

return 0; }

----------------------------------------------------------------------------------------------------------------------------

3. The following is the starter code (p03.c):

#include int main(){ printf("Program 03 - No Name "); return 0; }

----------------------------------------------------------------------------------------------------------------------------

4. The following is the example output:

> p03 10 Program 03 - No Name 10 darts : pi = 3.2000000000

Note: "> p03 10" is what has been entered into the terminal after compiling the program, the lines after it is the output. (see question above)

----------------------------------------------------------------------------------------------------------------------------

5. The following is the correct output values:

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

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

Modern Database Management

Authors: Fred R. McFadden, Jeffrey Slater, Mary B. Prescott

5th Edition

0805360549, 978-0805360547

More Books

Students also viewed these Databases questions