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
  • Convert this code from C to C++ include <stdio.h> include <locale.h> void filtre (double x. double yll, double yol, int , int N) int Nn-10000 int main) setlocale (LC ALL,") double...

    Convert this code from C to C++ include <stdio.h> include <locale.h> void filtre (double x. double yll, double yol, int , int N) int Nn-10000 int main) setlocale (LC ALL,") double x(Nm) , y [ Nm],yo Nml®(0.0); int m; FILE dosyal-fopen("D:/veri/673.txtr FILE dosya2-fopen("D:/veri/data2.txt int i-0 while( feof (dosyal)) fscanf (dosyal, " ",xi sytil) fclose (dosyal) int Nij printf("Pencere Genişligi scanf() 3 filtre(x,y.Yo,m,N) for(int k-0keNkt+)fprintf (dosya2, 10.41 10.411 0.41fn",x[k],y[k].yo[k]) fclose(dosya2) void filtre (double x double y. double yoll, int m, int...

  • 1 Rewrite the following program so that it will use function. include <stdio.h> Int main) printf...

    1 Rewrite the following program so that it will use function. include <stdio.h> Int main) printf ("Hello!") return 0 2 Assume a program has the following declarations: short s- 4 int i--5; long m-3 float f-19.3f; double d-3.6; What are the following values? (a) s+m (b) (float) d (c) d s (e) (int) f 3 What will be the output of the following program: int x = 3 float a 2.76; y-2* x; print f("X-2d, y*ta, z»%d", x, y, z);...

  • #include <stdio.h> #include <stdlib.h> #include <math.h> int count,i, tracker = 0; int main() { scanf("%d\n", &count);...

    #include <stdio.h> #include <stdlib.h> #include <math.h> int count,i, tracker = 0; int main() { scanf("%d\n", &count); int numbers[count]; //GEtting the numbers and satisfy BS conditions for (i = 0; i < count; i++) { if (scanf("%d", &numbers[i]) != EOF) { tracker++; } if (numbers[i] < 0 || numbers[i] > 99) { printf("%d is not in the [0, 99] range.\n",numbers[i]); exit(i); } } if( tracker != count) { printf("%d numbers are required, but only %d were provided.\n", count, tracker); exit(1); }...

  • #include <stdio.h> void printHelp () { printf ("\n"); printf ("a: a(x) = x*x\n"); printf ("b: b(x)...

    #include <stdio.h> void printHelp () { printf ("\n"); printf ("a: a(x) = x*x\n"); printf ("b: b(x) = x*x*x\n"); printf ("c: c(x) = x^2 + 2*x + 7\n"); printf ("q: quit\n"); } void a(float x) { float v = x*x; printf (" a(%.2f) = %.2f^2 = %.2f\n", x, x, v); } // end function a void b(float x) { float v = x*x*x; printf (" a(%.2f) = %.2f^3 = %.2f\n", x, x, v); } // end function b void c(float x)...

  • MIPS assembly language Covert this code to MIPS: #include <stdio.h> int function (int a) int main)i...

    MIPS assembly language Covert this code to MIPS: #include <stdio.h> int function (int a) int main)i int x=5 ; int y: y function(x); printf "yd",y); return 0; int function (int a) return 3*a+5; Assumptions: . Place arguments in $a0-$a3 . Place return values in $vO-$v1 Return address saved automatically in $ra . lgnore the stack for this example. (Thus, the function will destroy registers used by calling function

  • convert this code from C to C++ include <stdio.h> include <locale.h> void filtre (double x. double...

    convert this code from C to C++ include <stdio.h> include <locale.h> void filtre (double x. double yll, double yol, int , int N) int Nn-10000 int main) setlocale (LC ALL,") double x(Nm) , y [ Nm],yo Nml®(0.0); int m; FILE dosyal-fopen("D:/veri/673.txtr FILE dosya2-fopen("D:/veri/data2.txt int i-0 while( feof (dosyal)) fscanf (dosyal, " ",xi sytil) fclose (dosyal) int Nij printf("Pencere Genişligi scanf() 3 filtre(x,y.Yo,m,N) for(int k-0keNkt+)fprintf (dosya2, 10.41 10.411 0.41fn",x[k],y[k].yo[k]) fclose(dosya2) void filtre (double x double y. double yoll, int m, int...

  • #include<stdio.h> int main() { int i; printf("Type an integer value: "); scanf("%d",&i); evaluate(i); return(0); } void...

    #include<stdio.h> int main() { int i; printf("Type an integer value: "); scanf("%d",&i); evaluate(i); return(0); } void evaluate(int x) { if(x > 10) printf("Value entered by you is greater than 10"); else if(x < 10) printf("Value entered by you is less than 10"); else printf("Value entered by you is equal 10"); } _______________________________________ report for this in hr ?

  • #include<stdio.h> int main() {       int data[10], i, j, temp;       printf("Enter 10 random number to...

    #include<stdio.h> int main() {       int data[10], i, j, temp;       printf("Enter 10 random number to sort in ascending order:\n");       for(i = 0; i < 10; i++)             scanf("%d", &data[i]);       /* Sorting process start */     ****** INSERT YOUR CODE TO COMPLETE THE PROGRAM ******       printf("After sort\n");       for(i = 0; i < 10; i++)             printf("%d\n",data[i]);       return 0; } Looking for alternative solutions for the program code.

  • Code that needs to be modified: #include <stdio.h> #include <math.h> int main(void) { FILE * fileout; int ix,iy,it,Nt; double newV[31][21],V[31][21],x,dx,y,dy; fileout=fopen("buckbeak....

    Code that needs to be modified: #include <stdio.h> #include <math.h> int main(void) { FILE * fileout; int ix,iy,it,Nt; double newV[31][21],V[31][21],x,dx,y,dy; fileout=fopen("buckbeak.dat","w"); printf("\nEnter number of iterations "); scanf("%d",&Nt); dx=0.10; dy=0.10;    for(iy=0; iy<21; iy=iy+1) { for(ix=0; ix<31; ix=ix+1) { V[ix][iy]=0.0; } } iy=20; for (ix=0;ix<31; ix=ix+1)    {    V[ix][iy]=8.0;    } for (it=0; it<Nt;it=it+1) { for(iy=1; iy<20;iy=iy+1) { for(ix=1;ix<30; ix=ix+1) { newV[ix][iy]=0.25*(V[ix+1][iy]+V[ix-1][iy]+V[ix][iy+1]+V[ix][iy-1]); } } for (iy=1; iy<20; iy=iy+1) { for(ix=1; ix<30; ix=ix+1) { V[ix][iy]=newV[ix][iy]; } }    } for (iy=0;iy<21;iy=iy+1) { y=dy*iy; for (ix=0;ix<31;ix=ix+1) {...

  • i need flowchart for the following c code #include <stdio.h> int main() {     int n,...

    i need flowchart for the following c code #include <stdio.h> int main() {     int n, i, sum = 0;     printf("Enter an integer: ");     scanf("%d",&n);     i = 1;     while ( i <=n )     {         sum += i;         ++i;     }     printf("Sum = %d",sum);     return 0; }

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