1

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...

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.

Answers

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


Similar Solved Questions

1 answers
EkSGjuupP229 TerShrArvkmkAMsqqCbac3hUGPBRNch1Wh2YVyNoby90F)dXGZT2GVdsz9bG5RrrinOqNBLLW%3d%3d8.classid=2293815#/learning Platform/class/s Topic 5 DQ 1 You are a hospital administrator, and you receive a...
EkSGjuupP229 TerShrArvkmkAMsqqCbac3hUGPBRNch1Wh2YVyNoby90F)dXGZT2GVdsz9bG5RrrinOqNBLLW%3d%3d8.classid=2293815#/learning Platform/class/s Topic 5 DQ 1 You are a hospital administrator, and you receive a call from a colleague at another hospital. Your colleague, who is a friend, informs you that he ha...
1 answers
A) Iriz's stock is currently selling for RM160.00 per share and the firm's dividends are expected...
a) Iriz's stock is currently selling for RM160.00 per share and the firm's dividends are expected to grow at 5% indefinitely. In addition, Iriz's most recent dividend was RM5.50. The expected risk-free rate of return is 3%, the expected market return is 8%, and Iriz has a beta of 1.20. R...
1 answers
What is the amount of the annual coupon payment for a bond that has 6 years...
What is the amount of the annual coupon payment for a bond that has 6 years until maturity, sells for $1,050, and has a yield to maturity of 9.37%?...
1 answers
In 1968, Ford decided to develop firstly a new mini car called Pinto, aiming to challenge...
In 1968, Ford decided to develop firstly a new mini car called Pinto, aiming to challenge the Beetle in American market. The price for Pinto can't be over 2000 US dollars; R&D department found the distance between the oil tank and bumper is only 10 inches. Tailgate collision experiment indic...
1 answers
Objective: Your company (or a made-up company) is considering moving toward/away from an open office desk...
Objective: Your company (or a made-up company) is considering moving toward/away from an open office desk arrangement. Provide a business proposal to your supervisor as to why your company should (or shouldn't) make the switch using at least 3 outside sources. Please use APA formatting in additi...
1 answers
Design a circuit that has a 3 bit binary input (representing 0 through 7) and outputs...
Design a circuit that has a 3 bit binary input (representing 0 through 7) and outputs a 1 if the input is a prime number.  A prime number (or prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. a)  Fill out a truth table that represent...
1 answers
Correctly label the components of the upper respiratory tract. 8 02:11:00 Septal cartilage Vomer Perpendicular plate...
Correctly label the components of the upper respiratory tract. 8 02:11:00 Septal cartilage Vomer Perpendicular plate Oropharynx Nasopharynx Laryngopharynx Reset Zoom...
1 answers
Assume that Nike and Adidas are the only sellers of athletic footwear in the United States....
Assume that Nike and Adidas are the only sellers of athletic footwear in the United States. They are deciding how much to charge for similar shoes. The choices are “High” (H) and “Outrageously High” (OH). The payoff matrix is as follows Nike's dominant strategy is to ...
1 answers
A3. What is the Voltage gain of the following arrangement? A B
A3. What is the Voltage gain of the following arrangement? A B...
1 answers
Name and describe briefly the 3 Theoretical Models of Human Evolution, including the researchers associated with...
Name and describe briefly the 3 Theoretical Models of Human Evolution, including the researchers associated with each one?...
1 answers
NATO and the Warsaw Pact were two competing alliances during which time period?
NATO and the Warsaw Pact were two competing alliances during which time period?...
1 answers
A company's fixed operating costs are $620,000, its variable costs are $2.55 per unit, and the...
A company's fixed operating costs are $620,000, its variable costs are $2.55 per unit, and the product's sales price is $4.45. What is the company's break-even point; that is, at what unit sales volume will its income equal its costs? Round your answer to the nearest whole number....
1 answers
1. David is the designated beneficiary of a 529 plan. This year, he took a distribution...
1. David is the designated beneficiary of a 529 plan. This year, he took a distribution of $10,000 from the plan to pay down his student loan ($9,500 in principal and $500 interest). This is the only payment he made on his student loan this year. He received a Form 1098-E showing $500 in student loa...
1 answers
Independent random samples were selected from two binomial populations, with sample sizes and the number of...
Independent random samples were selected from two binomial populations, with sample sizes and the number of successes given below. Population 1 2 500 500 120 147 Sample Size Number of Successes Construct a 95% confidence interval for the difference in the population proportions. (Use P, - Pg. Round ...
1 answers
Using the CRC polynomial 1011, compute the CRC code word for the information word 1100011. Check...
Using the CRC polynomial 1011, compute the CRC code word for the information word 1100011. Check the division performed at the receiver....
1 answers
3) When the SNS increases cardiac output, a series of events occurs through innervation via adrenergic receptor...
3) When the SNS increases cardiac output, a series of events occurs through innervation via adrenergic receptors to affect both autorhythmic cells and contractile fibers. Describe and explain the changes in the function of the two cell types as cardiac output is increased, including all the membrane...

-- 0.008795--