Practical Examination Problem #65 Introduction The Monte-Carlo method combines the operation of a computer with the intentional injection of random data. A Monte-Carlo simulation attempts to...

Alorithm, and program, solving the problem. I have included the random.h file.


Practical Examination Problem #65 Introduction The Monte-Carlo method combines the operation of a computer with the intentional injection of random data. A Monte-Carlo simulation attempts to demonstrate the approximate solution to a mathematical problem using the random sampling techniques of the Monte-Carlo method. For example, when one “throws” a coin trials times and counts the number of heads and the number of tails, the observed quotient of heads-to-tails, (heads tails) 1.0 should empirically demonstrate the probabilities associated with tossing a fair 2-sided coin when trials is a large enough value to guarantee The Law of Large Numbers (LLN). From my dictionary, “In probability theory, the law of large numbers (LLN) is a theorem that describes the result of performing the same experiment a large number of times. According to the law, the average of the results obtained from a large number of trials should be close to the expected value, and will tend to become closer as more trials are performed.”(see https://en.wikipedia.org/wiki/Monte_Carlo_method and https://en.wikipedia.org/wiki/Law_of_large_numbers on August 13, 2017 for more details). Problem Input 2 integers, (1) T that specifies the number of times the RunOneTrial() executes before results are reported; and (2) n that specifies the size of the integer vectors (a 1-dimensional arrays), xs1[] and xs2[]. Execute the RunOneTrial() algorithm (shown below) T times while collecting 3 statistics, minimum-of-count, maximum-of-count, and the average-of-count. Simulation results must be reported using this format 11111111112222222222333333333344444444445555555555666666666677777777778 12345678901234567890123456789012345678901234567890123456789012345678901234567890 minimum maximum average n! -------------------- -------------------- ------------- -------------------- XXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXX XXXXXXXXXX.XX XXXXXXXXXXXXXXXXXXXX Algorithm RunOneTrial(OUT count,IN n) // Count the number of times xs1[] and xs2[] are re-filled with integers randomly-chosen // without replacement from [ 1,n ] until (xs1 = xs2). 1. dynamically-allocate an (n+1)-sized vector of integers named xs1 (ignore xs1[0] and xs2[0]) 2. dynamically-allocate an (n+1)-sized vector of integers named xs2 3. set count to 0 4. LOOP 5. fill xs1[i] i = 1,2,…,n with integers randomly-chosen without replacement from [ 1,n ] 6. fill xs2[i] i = 1,2,…,n with integers randomly-chosen without replacement from [ 1,n ] 7. increment count 8. UNTIL ( xs1 = xs2 ) 9. dynamically-deallocate vectors xs1 and xs2 Note There are n! permutations of the contents of xs1[] and of xs2[], there are (n!)2 distinct combinations of the permutations of xs1[] and xs2[]. n! of these (n!)2 combinations are equal, so theoretically, the “average” should compute to be n! for a large enough value of T. For example, for n = 3, the 3! = 3*2*1 = 6 permutations are 123, 132, 213, 231, 312, 321 and “average” should be about 6.0 for a large enough value of T. What are the (3!)2 = 62 = 36 combinations for the n = 3 case?! Sample Program Dialog Computational and Critical Thinking Questions 1. Which source files must the Problem #1 project contain? Hint See Dr. Hanna’s Dev-C++ IDE screenshot below. Select all that apply. □ Problem1.c □ Random.c □ Random.h □ stdio.h □ stdlib.h □ stdbool.h 2. (Continuing 1) Do you know how the preprocessor statement #include "..\Random.h" must be changed when the Random.h file is relocated in the directory structure? 3. What is the EQ formal parameter defined in the RunOneTrial() function prototype shown below? void RunOneTrial(unsigned long long int *count,const int n, bool (*EQ)(const int xs1[],const int xs2[],const int n)); Hint bool EQ(const int xs1[],const int xs2[], const int n); 4. (Continuing 3) What is the actual parameter passed to the formal parameter EQ when RunOneTrial() is called by (referenced by) main()? 5. What is sizeof(unsigned long long int)? ______ bytes = ______ bits Hint 1 byte = 8 bits. Which format specifier is used by printf() to display an unsigned long long int value? ______ What is the range of unsigned long long int? [ 0,2______-1 ] What is sizeof(signed long long int)? ______ byte = ______ bits T or F? The data types long long int and signed long long int are exactly the same data type. Which format specifier is used by printf() to display a long long int value? ______ What is the range of long long int? [ -2______,2______-1 ] Why use the data type long long int instead of your “old friend” int? Hint What is the range of int? [ -231,231-1 ] 6. Why can’t the main() local variables int T,n; can be defined inside the while-statement body where count is defined? 7. What appears to be a “large enough value of T”? ______ 8. What side-effect does the reference in main() to the random-related pseudolibrary function SetRandomSeed() accomplish? 9. Explain how the main() while-not-EOF-loop works. printf("T n? "); while ( scanf("%d%d",&T,&n) != EOF ) { ... printf("\nT n? "); } 10. The i-is-1 case associated with the main() for-statement is an example of a boundary case: How is the boundary case handled? What is another way to handle the boundary case? 11. Express the algorithm that is used by main() for finding the minimum (or maximum) of a series of 1 or more values. Do the same for the algorithm that is used by main() for finding a sum of a series of 1 or more numeric values. 12. Help for “Student provides missing code #1” By definition, xs1 and xs2 are equal when all corresponding elements are equal; that is (xs1 = xs2) is expressed using the universal quantifier, , like this (xs1[i] = xs2[i]) i { 1,2,...,n } which is just a mathematical shorthand for the boolean expression (xs1[1] = xs2[1]) AND (xs1[2] = xs2[2]) AND ... AND (xs1[n] = xs2[n]) Hint The AND (conjunction) operator is expressed in C as ______ and the = (comparison for equals) operator is expressed in C as ______. What kind of looping statement—for, while, or do/while—makes the best sense for “rolling-up the loop” expressed as a series of conjunctions? 13. Help for “Student provides missing code #2” n! is defined as follows 0! = 1, 1! = 1, and n! = 1*2*…*(n-1)*n for n 2. Hint This is just an example of rolling series of multiplications into to loop! 14. Consider the following code segment taken from RunOneTrial(): Which approach better suits your programming style, the xs1[] approach or the xs2[] approach? int *xs1 = (int *) malloc(sizeof(int)*(n+1)); int *xs2; xs2 = (int *) malloc(sizeof(int)*(n+1)); 15. (Continuing 14) Explain what (int *) malloc(sizeof(int)*(n+1)) “computes”. Don’t forget to explain why an array designed to have n elements is allocated with (n+1). Where do the pointer-to int variables xs1 and xs2 point-to? 16. (Continuing 14) T or F? The cast operator (int *) is absolutely necessary. Hint Look into standard library file or visit http://www.cplusplus.com/reference/cstdlib/malloc/ to determine that the data type of the pointer malloc() returns is vo__________. 17. Explain what *count = 0; means. Hint Your answer should use the term “indirection operator” or “dereferencing operator”. This statement is an expression-statement that includes 2 operators that have different syntax and different semantics. Which operator has the higher precedence? ______ T or F? Both operators, * and =, provide a side-effect. T or F? Both operators have the same associativity. The arity of the * operator is ______ and of the = operator is ______. 18. How is the semantics for the LOOP-UNTIL construct given in the algorithm for RunOneTrial(OUT count,IN n) different from the do/while-statement used in RunOneTrial()? do { RandomlyFillWithoutReplacement(xs1,n); RandomlyFillWithoutReplacement(xs2,n); (*count)++; } while ( !EQ(xs1,xs2,n) ); 19. (Continuing 18) The syntax (*EQ)(xs1,xs2,n) that can be used to reference the function pointed-to by EQ is semantically-equivalent to the syntax given above, but one of the syntaxes better emphasizes the “pointer-to-ness” of EQ: which one? 20. (Continuing 18) Give at least 2 other syntaxes to use to increment the integer that count points-to that are “prettier” than this, (*count)++;. 21. When either or both of the free() function references shown below are deleted, a resource leak would occur. Under what circumstances would the memory leak be dangerous? free(xs1); free(xs2); 22. T or F? The curly-brace pair {} that “sandwich” the body of the do/while-statement is absolutely necessary. for(i = 1; i <= n; i++) { int x; do { x = randominteger(1,n); } while ( xinxs(xs,i-1,x) ); xs[i] = x; } 23. (continuing 22) how is the body of the for-statement simplified when the problem #1 requirements change to allow “integers randomly-chosen with replacement from [ 1,n ]”? make the change temporarily: do you get approximately the same results for large enough t? if your answer is no, attempt a “discrete-mathy” probabilistic analysis to try to explain the results. hint does nn grow faster than n!? 24. (continuing 22) what does the reference in main() to the random-related pseudolibrary function randominteger() return? your answer should use the phrase “uniformly-distributed”. 25. (continuing 22) why does the reference to xinxs() use the expression i-1 as the second actual parameter instead of either i or n? 26. help for “student provides missing code #3” by definition, x is “in” xs[] when there exists at least 1 element of xs[], xs[i], i [ 1,n ], for which (xs[i] = x). this is expressed using the existential quantifier, , like this (xs[i] = x) i { 1,2,...,n } which is just a mathematical shorthand for the boolean expression (xs1[1] = x) or (xs[2] = x]) or ... or (xs1[n] = x) hint the or (disjunction) operator is expressed in c as ______ and the = (comparison for equals) operator is expressed in c as ______. what kind of looping statement—for, while, or do/while—makes the best sense for “rolling-up the loop” expressed as a series of disjunctions? 27. draw a structure chart that depicts the software architecture of problem1.c. 28. explain the conceptual basis for making a monte-carlo simulation. 29. explain the law of large numbers in your own words. 30. n;="" i++)="" {="" int="" x;="" do="" {="" x="RandomInteger(1,n);" }="" while="" (="" xinxs(xs,i-1,x)="" );="" xs[i]="x;" }="" 23.="" (continuing="" 22)="" how="" is="" the="" body="" of="" the="" for-statement="" simplified="" when="" the="" problem="" #1="" requirements="" change="" to="" allow="" “integers="" randomly-chosen="" with="" replacement="" from="" [="" 1,n="" ]”?="" make="" the="" change="" temporarily:="" do="" you="" get="" approximately="" the="" same="" results="" for="" large="" enough="" t?="" if="" your="" answer="" is="" no,="" attempt="" a="" “discrete-mathy”="" probabilistic="" analysis="" to="" try="" to="" explain="" the="" results.="" hint="" does="" nn="" grow="" faster="" than="" n!?="" 24.="" (continuing="" 22)="" what="" does="" the="" reference="" in="" main()="" to="" the="" random-related="" pseudolibrary="" function="" randominteger()="" return?="" your="" answer="" should="" use="" the="" phrase="" “uniformly-distributed”.="" 25.="" (continuing="" 22)="" why="" does="" the="" reference="" to="" xinxs()="" use="" the="" expression="" i-1="" as="" the="" second="" actual="" parameter="" instead="" of="" either="" i="" or="" n?="" 26.="" help="" for="" “student="" provides="" missing="" code="" #3”="" by="" definition,="" x="" is="" “in”="" xs[]="" when="" there="" exists="" at="" least="" 1="" element="" of="" xs[],="" xs[i],="" i="" [="" 1,n="" ],="" for="" which="" (xs[i]="x)." this="" is="" expressed="" using="" the="" existential="" quantifier,="" ,="" like="" this="" (xs[i]="x)" i="" {="" 1,2,...,n="" }="" which="" is="" just="" a="" mathematical="" shorthand="" for="" the="" boolean="" expression="" (xs1[1]="x)" or="" (xs[2]="x])" or="" ...="" or="" (xs1[n]="x)" hint="" the="" or="" (disjunction)="" operator="" is="" expressed="" in="" c="" as="" ______="" and="" the="(comparison" for="" equals)="" operator="" is="" expressed="" in="" c="" as="" ______.="" what="" kind="" of="" looping="" statement—for,="" while,="" or="" do/while—makes="" the="" best="" sense="" for="" “rolling-up="" the="" loop”="" expressed="" as="" a="" series="" of="" disjunctions?="" 27.="" draw="" a="" structure="" chart="" that="" depicts="" the="" software="" architecture="" of="" problem1.c.="" 28.="" explain="" the="" conceptual="" basis="" for="" making="" a="" monte-carlo="" simulation.="" 29.="" explain="" the="" law="" of="" large="" numbers="" in="" your="" own="" words.="">
Aug 17, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here