Question

Adapt the code and run it (in the language of your choice). Fill in the table. Show all theoretical calculations. Attach actual simulation code and output. 100 INPUT 200 FOR i1 TO 1000 300 FOR 1 To n generate x 400 SSX 500 NEXT 600 IF (S/n > 0.45) AND (S/n < 0.55) THEN c = c * 1 700 S0 800 NEXT 900 PRINT c/1000 P(O.45< Tn) 0.55) 100 simulation theory CLT) simulation theory CLT simulation theory (exact) epr2 P(X = 0.2) = 0.8 PD

thumbs up ONLY for correct answer.
thank you.

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

Hi,

I am using C to implement the above psuedo code since it is widely used and easy to understand.

#include <stdio.h>

int main()
{
float n;
float s=0;
float x;
float c=0;
printf("Input n \n");
scanf("%f", &n);
for(int i =1; i<=1000; i++){
for(int j=1; j<=n; j++){
x = 0.5;
s = s+x;
}
if((s/n>0.45)&&(s/n<0.55)){
c = c+1;
s=0;
}

}
float a = c/1000;
printf("%f",a);
return 0;
}

Or we can use

#include <stdio.h>
#include<stdlib.h>
int main()
{
float n;
float s=0;
float x;
float c=0;
printf("Input n \n");
scanf("%f", &n);
for(int i =1; i<=1000; i++){
for(int j=1; j<=n; j++){
x = (((float) rand() / RAND_MAX) * 0.10) + 0.45; //generating random decimal between 0.45 and 0.55
s = s+x;
}
if((s/n>0.45)&&(s/n<0.55)){
c = c+1;
s=0;
}

}
float a = c/1000;
printf("%f",a);
return 0;
}

  • First I have declared and initialized the variables and then I have asked to input n
  • Then iterate through loop 1000 times
  • Iterate the nested loop n times
  • Generate the value of x, in the first code I have hard coded the value of x and in the second I have generated it using the rand function defined in stdlib to generate random number. x = (((float) rand() / RAND_MAX) * 0.10) + 0.45; Basically '(float) rand() / RAND_MAX)' part is generating a random decimal number. and it is multiplied by 0.10 which is difference between 0.55 and 0.45. Since, we want to take a random number between 0.45 and 0.55. and the decimal is added to 0.45 to ensure the value is grater than 0.45 and less than 0.55.
  • Add x to s which is nothing but sum of x's through out the loop
  • After coming out of the nested loop, if condition is checked if it is true,, c is incremented and similarly it will exit the outer loop also.
  • Finally, print c/1000

Find attached the screen shots of different outputs with 3 different values of n. n=100, n=25, n=1main.c nt main 13 14 15 16 17 18 19 20- 21 float n; float s-0; float x; float c-e printf(Input n In); scanf(%f, &n); for(input nput n 25 1.000000 ..Program finished with exit code 0 Press ENTER to exit console.input Input n 1.000000 . Program finished with exit code 0 Press ENTER to exit console.I hope this will help!!!!!!

Thanks!!!

Add a comment
Know the answer?
Add Answer to:
thumbs up ONLY for correct answer. thank you. Adapt the code and run it (in the...
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