Question: THIS IS ONE SINGLE ASSIGNMENT PLEASE DO BOTH PARTS.(ITS ONE SINGLE LAB) I need this in c programming assignment is given and also code is
THIS IS ONE SINGLE ASSIGNMENT PLEASE DO BOTH PARTS.(ITS ONE SINGLE LAB)
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
please do both files as this is part of one single assignment

home / study / engineering / computer science / computer science questions and answers / this is one single assignment please do both parts.(its one single lab) i need this in c programming...
Your question has expired and been refunded.
We were unable to find a Chegg Expert to answer your question.
Question: THIS IS ONE SINGLE ASSIGNMENT PLEASE DO BOTH PARTS.(ITS ONE SINGLE LAB) I need this in c programm...
THIS IS ONE SINGLE ASSIGNMENT PLEASE DO BOTH PARTS.(ITS ONE SINGLE LAB)
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
please do both files as this is part of one single assignment

below is makefile

below is 2d-random.c


#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
---
below is strfuncs.c




#include #include
//You might need the below function //This function returns the length of a string //It is equivlent to strlen() in string.h
int lenstr(char* s) { int len = 0; while(s[len++] != ''); return len-1; }
/** Appends the src string to the end of the * dest string. Return the resulting string. */
char* strcat(char dest[], char src[]) { //Add your code here
}
/** Searches the haystack string for the needle * substring. Return a pointer to the located * needle substring in the haystack string if * it exists (and the first if there are more * than one), or NULL if the needle is not in * the haystack. */
char* strstr(char haystack[], char needle[]) {
//Add your code here
}
/** Searches for the first occurrence of the * character c in the string s and returns a * pointer to it. If c does not appear in s, * return NULL. */
char* str_chr(char s[], char c) {
//Add your code here
}
/** Returns a pointer to a new string which is * a copy of the given string s. */
char* strdup(char s[]) { //Add your code here
}
/** Returns 1 if the strings s1 and s2 are the * same, returns 0 otherwise. */
int streq(char s1[], char s2[]) { //Add your code here
}
/** Main function. Add code to free allocated memory! * Valgrind should NOT yield any errors once you're done! * DO NOT CHANGE OUTPUTS! JUST ADD CLEAN UP CODE! */ int main(int argc, char** argv) { /* Read strings from program arguments */ if(argc != 3) { printf("usage: ./strfuncs s1 s2 "); return 1; } char* s1 = argv[1]; char* s2 = argv[2]; printf("String 1: %s ", s1); printf("String 2: %s ", s2); /* Check for string equality */ int s1eqs2 = streq(s1, s2); printf("s1=s2? %s ", s1eqs2 ? "yes" : "no"); /* Concatenate s1 to s2 and s2 to s1 */ char* s1s2 = strcat(s1, s2); char* s2s1 = strcat(s2, s1); printf("s1+s2=%s ", s1s2); printf("s2+s1=%s ", s2s1); /* Check for substrings */ char* s1ins2 = strstr(s2, s1); char* s2ins1 = strstr(s1, s2); printf("s1 in s2 -> %s ", s1ins2 == NULL ? "no" : "yes"); printf("s2 in s1 -> %s ", s2ins1 == NULL ? "no" : "yes"); /* Check for character occurence */ char* ains1 = str_chr(s1, 'a'); printf("'a' in s1? %s ", ains1 == NULL ? "no" : "yes"); /* Check duplication of strings */ char* dups1 = strdup(s1); printf("dup(s1)=%s ", dups1); /* Clean up, i.e. free memory! */ //Add your code here
/* Done! */ return 0; }
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), (n, n), 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 Exercise 2. (50 points) DIY String Functions In this assignment, we will implement five string functions ourselves The five functions to implement are strcat, strstr, strchr, strdup, and streq. Documentation for each function is provided in strfuncs.c. Some of these functions already have implementations in C; These functions are not allowed for this assignment! We must implement each of these functions without using any built-in string functions. Any use of built-in string functions will result in no credit for this assignment Once we have implemented these five functions, we should also add some code to the main function to free any memory our functions may have allocated when invoked. Add this code at the end of the function under the relevant comment. Valgrind should not report any errors or memory leaks! A quick start guide for Valgrind can be found at http: //valgrind. org/ docs/manual/ quick-start. html 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), (n, n), 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 Exercise 2. (50 points) DIY String Functions In this assignment, we will implement five string functions ourselves The five functions to implement are strcat, strstr, strchr, strdup, and streq. Documentation for each function is provided in strfuncs.c. Some of these functions already have implementations in C; These functions are not allowed for this assignment! We must implement each of these functions without using any built-in string functions. Any use of built-in string functions will result in no credit for this assignment Once we have implemented these five functions, we should also add some code to the main function to free any memory our functions may have allocated when invoked. Add this code at the end of the function under the relevant comment. Valgrind should not report any errors or memory leaks! A quick start guide for Valgrind can be found at http: //valgrind. org/ docs/manual/ quick-start. html #define more variables so it is easier to make changes CC-gcc CFLAGS=-g -Wall -std=c39 TARGETS-2d-random strfuncs all: $ (TARGETS) 2d-random: 2d-random.c $ (CC) $ (CFLAGS) -o Se e.c strfuncs: strfuncs.c $ (CC) $ (CFLAGS) -o Se e.c clean: rm -rf *.o *~$ (TARGETS) 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); 02d-random-c strfuncs.c x include 2 #include 3 4 //You might need the below function 5 //This function returns the length of a string 6 //It is equivlent to strlen) in string.h 8 int lenstr(char* s) f 9 int len 0; 10 while(s[Len++]1e return Len-1; 14/*Appends the src string to the end of the 15 dest string. Return the resulting string.*/ 16 17 char* strcat (char dest[], char src[]) 18 19 20 21 //Add your code here 23 24 25 26 /x Searches the haystack string for the needle 27 substring. Return a pointer to the located 28 needle substring in the haystack string if 29it exists (and the first if there are more 30 than one), or NULL if the needle is not in 31 the haystack./ 32 33 char* strstr(char haystack[], char needlel]) f 34 35 36 37 38 39 40 41 42 //Add your code here C 2d-random.c strfuncs.cx 43/** Searches for the first occurrence of the 44 character c in the string s and returns a 45 pointer to it. If c does not appear in s, 46 return NULL* 47 48 char* str_chr (char s[], char c) 49 50 /Add your code here 51 52 53 54 56 57/xReturns a pointer to a new string which is 58 a copy of the given string s.*/ 59 60 char strdup (char s[]) 61 62 63 64 65 //Add your code here 67 68 69 70Returns 1 if the strings s1 and s2 are the 71 same, returns otherwise.* 72 73 int streq(char s1[], char s2[]) 74 75 76 //Add your code here 78 79 80 81 82 83 84 /*x Main function. Add code to free allocated memory! C 2d-random.c strfuncs.c x 84/x Main function. Add code to free allocated memory! 85 Valgrind should NOT yield any errors once you're done! 86 DO NOT CHANGE OUTPUTS! JUST ADD CLEAN UP CODE! / 87 int main(int argc, char** argv) /* Read strings from program arguments/ if (argc!3) 89 printf("usage: ./strfuncs sl s2 "); return 1; 91 92 93 94 95 96 97 98 char* s1 = argv [1]; chark s2 argv[2]; printf("String 1: %s ", s1); printf("String 2: %s ", s2); /* Check for string equality int sleqs2 streq (s1, s2); printf("s1-s2? %s ", sleqs2 ? "yes" : "no"); 102 103 104 105 106 107 108 /* Concatenate sl to s2 and s2 char* s1s2 strcat (s1, s2); char* s2s1 = strcat (s2, s1); printf("s1+s2-%s ", s1s2); printf("s2+s1-%s ", s2s1); to sl / /* Check for substrings/ char* slins2strstr (s2, sl); char* s2insl -strstr (sl, s2); printf("s1 in s2-> %s ", siins2 == NULL ? "no" "yes"); printf("s2 in s1-> %s ", s2ins1-NULL ? "no" : "yes"); 110 112 113 114 115 116 /*Check for character occurence/ char* ainsl str_chr (s1, 'a' printf(" ' a ' in s12 %s ", ains1 == NULL ? "no" : "yes"); 118 119 12 121 122 123 124 125 /* Check duplication of strings / char* dups1 = strdup(s1); printf("dup(s1)-%s ", dupsi); /* Clean up, i.e.free memory!*/ //Add your code here 123 /* Clean up, i.e. free memory!/ 124 //Add your code here 125 126 127 Done! / 128 129 return ; 131 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), (n, n), 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 Exercise 2. (50 points) DIY String Functions In this assignment, we will implement five string functions ourselves The five functions to implement are strcat, strstr, strchr, strdup, and streq. Documentation for each function is provided in strfuncs.c. Some of these functions already have implementations in C; These functions are not allowed for this assignment! We must implement each of these functions without using any built-in string functions. Any use of built-in string functions will result in no credit for this assignment Once we have implemented these five functions, we should also add some code to the main function to free any memory our functions may have allocated when invoked. Add this code at the end of the function under the relevant comment. Valgrind should not report any errors or memory leaks! A quick start guide for Valgrind can be found at http: //valgrind. org/ docs/manual/ quick-start. html 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), (n, n), 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 Exercise 2. (50 points) DIY String Functions In this assignment, we will implement five string functions ourselves The five functions to implement are strcat, strstr, strchr, strdup, and streq. Documentation for each function is provided in strfuncs.c. Some of these functions already have implementations in C; These functions are not allowed for this assignment! We must implement each of these functions without using any built-in string functions. Any use of built-in string functions will result in no credit for this assignment Once we have implemented these five functions, we should also add some code to the main function to free any memory our functions may have allocated when invoked. Add this code at the end of the function under the relevant comment. Valgrind should not report any errors or memory leaks! A quick start guide for Valgrind can be found at http: //valgrind. org/ docs/manual/ quick-start. html #define more variables so it is easier to make changes CC-gcc CFLAGS=-g -Wall -std=c39 TARGETS-2d-random strfuncs all: $ (TARGETS) 2d-random: 2d-random.c $ (CC) $ (CFLAGS) -o Se e.c strfuncs: strfuncs.c $ (CC) $ (CFLAGS) -o Se e.c clean: rm -rf *.o *~$ (TARGETS) 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); 02d-random-c strfuncs.c x include 2 #include 3 4 //You might need the below function 5 //This function returns the length of a string 6 //It is equivlent to strlen) in string.h 8 int lenstr(char* s) f 9 int len 0; 10 while(s[Len++]1e return Len-1; 14/*Appends the src string to the end of the 15 dest string. Return the resulting string.*/ 16 17 char* strcat (char dest[], char src[]) 18 19 20 21 //Add your code here 23 24 25 26 /x Searches the haystack string for the needle 27 substring. Return a pointer to the located 28 needle substring in the haystack string if 29it exists (and the first if there are more 30 than one), or NULL if the needle is not in 31 the haystack./ 32 33 char* strstr(char haystack[], char needlel]) f 34 35 36 37 38 39 40 41 42 //Add your code here C 2d-random.c strfuncs.cx 43/** Searches for the first occurrence of the 44 character c in the string s and returns a 45 pointer to it. If c does not appear in s, 46 return NULL* 47 48 char* str_chr (char s[], char c) 49 50 /Add your code here 51 52 53 54 56 57/xReturns a pointer to a new string which is 58 a copy of the given string s.*/ 59 60 char strdup (char s[]) 61 62 63 64 65 //Add your code here 67 68 69 70Returns 1 if the strings s1 and s2 are the 71 same, returns otherwise.* 72 73 int streq(char s1[], char s2[]) 74 75 76 //Add your code here 78 79 80 81 82 83 84 /*x Main function. Add code to free allocated memory! C 2d-random.c strfuncs.c x 84/x Main function. Add code to free allocated memory! 85 Valgrind should NOT yield any errors once you're done! 86 DO NOT CHANGE OUTPUTS! JUST ADD CLEAN UP CODE! / 87 int main(int argc, char** argv) /* Read strings from program arguments/ if (argc!3) 89 printf("usage: ./strfuncs sl s2 "); return 1; 91 92 93 94 95 96 97 98 char* s1 = argv [1]; chark s2 argv[2]; printf("String 1: %s ", s1); printf("String 2: %s ", s2); /* Check for string equality int sleqs2 streq (s1, s2); printf("s1-s2? %s ", sleqs2 ? "yes" : "no"); 102 103 104 105 106 107 108 /* Concatenate sl to s2 and s2 char* s1s2 strcat (s1, s2); char* s2s1 = strcat (s2, s1); printf("s1+s2-%s ", s1s2); printf("s2+s1-%s ", s2s1); to sl / /* Check for substrings/ char* slins2strstr (s2, sl); char* s2insl -strstr (sl, s2); printf("s1 in s2-> %s ", siins2 == NULL ? "no" "yes"); printf("s2 in s1-> %s ", s2ins1-NULL ? "no" : "yes"); 110 112 113 114 115 116 /*Check for character occurence/ char* ainsl str_chr (s1, 'a' printf(" ' a ' in s12 %s ", ains1 == NULL ? "no" : "yes"); 118 119 12 121 122 123 124 125 /* Check duplication of strings / char* dups1 = strdup(s1); printf("dup(s1)-%s ", dupsi); /* Clean up, i.e.free memory!*/ //Add your code here 123 /* Clean up, i.e. free memory!/ 124 //Add your code here 125 126 127 Done! / 128 129 return ; 131