1 / 18

CS1010E Tutorial 8 Group A[09]

CS1010E Tutorial 8 Group A[09]. Question 2. Question 2). Simulate a drunkard’s walk for a number of random trials Determine how many times it lands at a specific location Drunkard’s Movement (Assuming he starts from position 0)

mason
Download Presentation

CS1010E Tutorial 8 Group A[09]

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1010ETutorial 8Group A[09] Question 2

  2. Question 2) • Simulate a drunkard’s walk for a number of random trials • Determine how many times it lands at a specific location • Drunkard’s Movement (Assuming he starts from position 0) • If 1 step – Ends up either at position +1 or -1 (1 forward step or 1 backward step) • If 2 steps – Ends up at either position +2, 0 or -2 (2 forward steps, 1 backward and 1 forward step or vice versa or 2 backward steps) • In general – If n number of steps, there would be n+1 different positions • Other things to note: • If n is odd, you will end up at an odd position • If n is even, you will end up at an even position

  3. Question 2a) • Create a Main Function declaring array path for 15 steps • Make sure all elements are zero (INITIALIZE) • Seed random no. generator #include <stdio.h> #include <time.h> #define MAX 31 int main(void) { int walk[MAX]={0}; srand(time(NULL)); return 0; }

  4. Question 2b) • Create a function readValues to read two values given by user • numTrials – number of simulation trials • numSteps – number of steps to take • Use void readValues(int *numTrials, int *numSteps) • Verify that the two variables hold the correct values read from user

  5. Question 2b) void readValues(int *numTrials, int *numSteps); int main (void) { int numTrials=0, numSteps=0; readValues(&numTrials,&numSteps); printf("%d %d\n", numTrials, numSteps); } void readValues(int *numTrials, int *numSteps) { int i, j; scanf("%d %d", &i, &j); *numTrials = i; *numSteps = j; return; }

  6. Question 2c) • Create a getDrunk function • This simulates numTrials trials of the walk of numSteps steps beginning at the reference point which is at the middle of the array • Performs forward and backward walk and does not regard the size of array • After each trial, the final landing position get an INCREMENT! • Every trial should begin at the same starting location • void getDrunk(int *path, intnumTrials, intnumSteps); • NOTE: • When passing in the array to the function, make sure that the right position is passed • In this case, I pass in walk[15] which is the middle of the array since this programme has to simulate front and back movement • This will ensure that for the required 15 steps, the walk will always be within the array

  7. Question 2c) void getDrunk(int *path, intnumTrials, intnumSteps); int main(void) { intnumTrials=0, numSteps=0, minValue; intwalk[MAX]={0}; getDrunk(&walk[15],numTrials,numSteps); … return 0; }

  8. Question 2c) void getDrunk(int *path, intnumTrials, intnumSteps) { intr, i, j, *temp; temp = path; for (i=1; i<=numTrials; i++) { for (j=1; j<=numSteps; j++) { r = rand()%2; if (r == 0) { path -= 1; } path = path + r; } *path += 1; path = temp; } return; }

  9. Question 2d) • Create a function printPath to print out NON-ZERO values of the array path (Basically the incremented values of the different positions that the drunkard landed • void printPath(int path[], int size); • How is printPath called from the main function??? • Use a function call in the main function called printPath • As we know from the previous function which increments the array at whichever position it lands, we send in that array into this function together with the size of the array which in this case is 31 • Test the 2 function in 2c) and 2d) for 10000 trials and 4 steps • OUTPUT roughly: 633 2539 3750 2465 613

  10. Question 2d) void printPath1(int path[], int size); int main(void) { intnumTrials=0, numSteps=0, minValue; intwalk[MAX]={0}; printPath1(walk,MAX); return 0; } void printPath1(int path[], int size) { inti; for (i=0;i<size;i++) { if (path[i] != 0) { printf("%d ", path[i]); } } printf("\n"); return; }

  11. Question 2e) • Create a findMin function to find the minimum NON-ZERO value in the path • For example: the minimum number in the sequence of: (633 2539 3750 2465 613) is 613! • intfindMin(int path[], int size); intfindMin(int path[], int size); int main (void) intnumTrials=0, numSteps=0, minValue; int walk[MAX]={0}; minValue = findMin(walk,MAX); printf("%d\n", minValue); return 0; }

  12. Question 2e) for (i=0;i<size;i++) { if (path[i] != 0) { if (path[i] <= minValue) { minValue = path[i]; } } } return minValue; } intfindMin(int path[], int size) { int *temp; inti, minValue; temp = path; while ((*temp) == 0) { temp = temp + 1; } minValue = *temp;

  13. Question 2f) • Modify the printPath function from 2d) so that every non-zero number is divided by the minimum no. that was obtain in 2e) and print out the output to the nearest integer • Eg. 633 2539 3750 2465 613 • The above numbers are all divided by the minimum value which is 613 • The output will be: 1 4 6 4 1

  14. Question 2f) void printPath2(int path[], int size, int min) { inti, intValue2; double intValue; for (i=0;i<size;i++) { if (path[i] != 0) { intValue = (path[i])/min; intValue2 = intValue + 0.5; printf("%d ", intValue2); } } printf("\n"); return; } void printPath2(int path[], int size, int min); int main(void) { intnumTrials=0, numSteps=0, minValue; intwalk[MAX]={0}; srand(time(NULL)); minValue= findMin(walk,MAX); printPath2(walk,MAX,minValue); return 0; }

  15. Question 2g) • Edit the main function such that we perform random walks from 0 to numSteps steps for the given number of numTrial trials • Then print out all of the above • For example if there are 4 steps of 10000 trial, we do 10000 trial for 0 steps, followed by 1 step, 2 step all the way till 4 steps, with each one having 10000 trials • A certain pattern can be observed (HINT: PASCAL NUMBERS ) • When we try out for different no. of trials we will observe that they give about the same output when the numbers are divided by the minimum value

  16. Question 2g) void readValues(int *numTrials, int *numSteps); void getDrunk(int *path, intnumTrials, intnumSteps); intfindMin(int path[], int size); void printPath2(int path[], int size, int min); int main(void) { intnumTrials=0, numSteps=0, minValue, i, j; intwalk[MAX]={0}; srand(time(NULL)); readValues(&numTrials,&numSteps); for (i=0;i<=numSteps;i++) { getDrunk(&walk[15],numTrials,i); minValue = findMin(walk,MAX); printPath2(walk,MAX,minValue); for (j=0;j<MAX;j++) { walk[j] = 0; } } return 0; }

  17. Question 2g) • From the above programme the output which is obtain for 10000 trials for 4 steps is: • 1 • 1 1 • 1 2 1 • 1 3 3 1 • 1 4 6 4 1 • This is like Pascal’s Triangle! (RECALL our Take-Home Labs and SIT-IN LABS!) • When you do for other values of trials as well as other values of steps, you will also observe a pattern similar to Pascal’s Triangle • It is only similar because, in this function we dealing with randomness hence there is some values which maybe off

  18. Question 2h) • After looking at the above example you can tell that actually there Is so much wastage of precious spaces in the arrays as we mostly only get even or odd values • For this bonus portion, we have to come up with a modified version of the Drunkard’s Walk such that there is efficient use of the arrays and less wastage • What I thought of was maybe to declare the numSteps first, followed by setting the array to that appropriate size so that we do not over use the function • However I’m not very sure! :p

More Related