Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need this in c programming assignment is given and also code is given you just have to code the lines where it says TODO

I need this in c programming

assignment is given and also code is given you just have to

code the lines where it says TODO here

please edit the code that i have given and please send me screenshot of the output as well

please only edit this below code shown. any other form will not work

image text in transcribed

image text in transcribed

image text in transcribed

#include #include

double two_d_random(int n) {

//Fill in code below //You are fee to write some other functions this function //Allocate memory for all the integer (x, y) coordinates inside the boundary //Allocate just one memory block //Figure out how to map between (x, y) and the index of the array //We can treat this allocated memory block as an integer array //Write a function for this mapping if needed.

//When deciding which way to go for the next step, generate a random number as follows. //r = rand() % 4; //Treat r = 0, 1, 2, 3 as moving up, right, down and left respectively.

//The random walk should stop once the x coordinate or y coordinate reaches $-n$ or $n$. //The function should return the fraction of the visited $(x, y)$ coordinates inside (not including) the square.

//Do not forget to free the allocated memory block before the function ends.

}

//Do not change the code below int main() { int trials = 1000;

srand(12345); for(int n=1; n Exercise 1. (50 points) Bounded 2D Random Walk In this problem we will write a program to simulate a 2D random walk. Imagine a random walker starting at the origin (0,0) and with equal probabilities goes up, right, down and left. For example, when the walker is at (z, y), with equal probability 1/4, his next location is at (x, y -1), (r + 1, y), (x, y +1), or (x -1,y) Given a positive integer n, a square is defined by the following four points: (-n,-n), (-n n), (nm), and (n, -n). We are interested in knowing on average, what fraction of points within this square the walker visits before he touches one of the edges of the square, given he starts his walk from (0, 0) One extreme example is n 1, when the walker starts from (0,0), after one step, he will touch one of the edges of the square. There is only 1 point inside the square and the walk visits this point before he touches one of the edges. Therefore, this faction is 1 for n 1 In the starter code 2d-random.c, implement the following functiorn double two d random (int n) This function should return the fraction mentioned above. n is the integer mentioned above to define the uare bounda sq rY In the function, we first allocate memory for all the integer (x, y) coordinates inside the square boundary. This is because we need to keep track which coordinates have been visited. Allocate just one memory block and figure out how we map between (x, y) to the index of the array(we can treat this allocated memory block as an integer array). We can write a function for this mapping if needed When deciding which way to go for the next step, generate a random number as follows r-rand () % 4; and treat r 0,1,2,3 as going up, right, down and left respectively The random walk should stop once the x coordinate or y coordinate reaches -n or n. The function should return the fraction of the visited (x, y) coordinates inside (not including) the square Reminder: Do not forget to free the allocated memory block before the function ends G 2d-random.cx Finclude 2 #include 3 4 double two_d_random (int n) 6 //Fill in code below //You are fee to write some other functions this function 8 9T/Allocate memory for all the integer (x, y) coordinates inside the 0 I/AlLocate just one memory block 12 13 /Write a function for this mapping if needed. boundary //Figure out how to map between (x, y) and the index of the array //We can treat this allocated memory block as an integer array 14 15 //when deciding which way to go for the next step, generate a random number as follows. //r rand ( ) % 4; //Treat r-e, 1, 2, 3 as moving up, right, down and left respectively 16 18 19 I/The random walk should stop once the x coordinate or y coordinate reaches $-ns or $n$. 20 I/The function should return the fraction of the visited $(x, y)$ coordinates inside (not including) the square 22 /Do not forget to free the allocated memory block before the function ends 23 24 25 26 27 28 29 30 31 32 //Do not change the code below 33 int main() 34 35 nt trials -1000; 36 37 srand (12345); 32 //Do not change the code below 33 int main() 34 35 36 37 38 39 int trials 10 ; srand (12345); for (int n-l; n 2 #include 3 4 double two_d_random (int n) 6 //Fill in code below //You are fee to write some other functions this function 8 9T/Allocate memory for all the integer (x, y) coordinates inside the 0 I/AlLocate just one memory block 12 13 /Write a function for this mapping if needed. boundary //Figure out how to map between (x, y) and the index of the array //We can treat this allocated memory block as an integer array 14 15 //when deciding which way to go for the next step, generate a random number as follows. //r rand ( ) % 4; //Treat r-e, 1, 2, 3 as moving up, right, down and left respectively 16 18 19 I/The random walk should stop once the x coordinate or y coordinate reaches $-ns or $n$. 20 I/The function should return the fraction of the visited $(x, y)$ coordinates inside (not including) the square 22 /Do not forget to free the allocated memory block before the function ends 23 24 25 26 27 28 29 30 31 32 //Do not change the code below 33 int main() 34 35 nt trials -1000; 36 37 srand (12345); 32 //Do not change the code below 33 int main() 34 35 36 37 38 39 int trials 10 ; srand (12345); for (int n-l; n

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Harness The Power Of Big Data The IBM Big Data Platform

Authors: Paul Zikopoulos, David Corrigan James Giles Thomas Deutsch Krishnan Parasuraman Dirk DeRoos Paul Zikopoulos

1st Edition

0071808183, 9780071808187

More Books

Students also viewed these Databases questions

Question

Describe the criteria for an effective budget.

Answered: 1 week ago