Question

Code that needs to be modified: #include #include #include int main() { int i,N,x; unsigned int seed; double R; printf("\nEnter number of iterations and seed"); printf("\n"); scanf(&#3...

Code that needs to be modified:

#include

#include

#include

int main()

{

int i,N,x;

unsigned int seed;

double R;

printf("\nEnter number of iterations and seed");

printf("\n");

scanf("%i %u", &N,&seed);

srand(seed);

  

for(i=0;i

{

R=(double)rand()/RAND_MAX;

if (R<0.5)

x=x+1;

else

x=x-1;

}

printf("Final location is ");

printf("%d",x);

printf("\n");

}

Question: Write a code that generates N pairs of random numbers. Call the first member of each pair x and the second member y. Count how many of the N pairs obey x^2+y^2<1. Call that number M, and have your code print out M/N. What do you get for M/N when N=10^2? How about N=10^4, N=10^6 and N=10^8? Explain your result. The language used is C.

Hint: It might be useful to multiply the value you get for M/N by four.

Warning: Be careful how you define N and M. If you have variables N,M that are integers and compute N/M the computer will give you an integer which is the number of times M foes into N. Thus in N=5 an M=3 then N/M=5/3=1. Similarly if N=13 and M=2 then N/M=13/2=6. Finally, if N=1 and M=2 then N/M=1/2=0. if you want 13/2=6.5 you need to tell the computer 13 is a double (or float) by declaring it as a double(or float). This is an important point to remember in coding. If you ever write a line of C code like y=(1/2)*x; the computer will set y=0 because it will compute 1/2=0 before multiplying by x.

******* Please make sure you answer all the questions asked and please do it 100%correctly. Please write neat and very clear that way i can be able to read and understand what is being said. The language used is C.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
double calculate(int n) //Function to calculate m
{
double x,y;
double sum;
int m=0,i;
for(i=0;i<n;i++)
{
x=(double)rand()/RAND_MAX;
y=(double)rand()/RAND_MAX;
double sum=(x*x)+(y*y);
if(sum<1)
m++;
}
double finite=(double)m/n;
return finite;
}
int main()
{
int i,N;
int m=0;
srand(time(NULL)); // Generate different number different time
double result100=calculate(100); //calling function for 10^2
printf("For 10^2 iterations%f\n",result100);
double result10000=calculate(10000); //calling function for 10^4
printf("For 10^4 iterations%f\n",result10000);
double result1000000=calculate(1000000); //calling function for 10^6
printf("For 100^6 iterations%f\n",result1000000);
double result100000000=calculate(100000000); // calling function for 10^8
printf("For 100^8 iterations%f\n",result100000000);
}

Output:

For 10^2 iterations0.890000
For 10^4 iterations0.786700
For 100^6 iterations0.785321
For 100^8 iterations0.785413
Add a comment
Know the answer?
Add Answer to:
Code that needs to be modified: #include #include #include int main() { int i,N,x; unsigned int seed; double R; printf("\nEnter number of iterations and seed"); printf("\n"); scanf(&#3...
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
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