Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Language is C Please read and follow instructions carefully. This is the existing code where the above functions need to be implemented. I have already

Language is C

Please read and follow instructions carefully.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

This is the existing code where the above functions need to be implemented. I have already implemented the functions mentioned in this code, just need help implementing the ones in the above screenshot.

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribedimage text in transcribed

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

This is the driver code file (code used to test the functions created from the above instructions) I don't know if I needed to include this but I did anyway, the code/functions MUST pass these tests:

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Please let me know if you need the imgUtils.c file code, or any further questions/clarifications. Thank you,

* 18 #include "imgutils.c" 19 20 // This lets the driver code override the image size if it needs to. Make sure 21 Il you don't hard-code these values anywhere! 22 #ifndef SIZEX 23 #define SIZEX 512 24 #define SIZEY 512 25 #endif 26 27 /* 28 29 30 * This struct contains one node of the linked list, which represents a single 31 * command to the Turtle. It's field should include: 32 * 33 cmd : A char array of size 10 holding the command name 34 35 - val : An integer that stores a parameter for the command (like forward, 36 backward and colour). 37 38 - next : A pointer to a struct of the same type, this is used for the 39 linked list 40 41 * TODO: Complete this struct definition 42 ****/ 43 44 typedef struct cmdnode { 45 char cmd [10]; 46 int val; 47 struct cmdnode *next; 48 } CmdNode; 49 50 * * * * * We're going to use the same data type (CmdNode) for the loop command, however we will have to add an additional field to keep track of all of the commands that are a part of the loop (in - you guessed it - another linked list!). The name for this command will simply be "loop", and the value it has will represent the number of iterations (how many times you will run all the commands in the loop). We won't quite be implementing all the functions from Part 1 here, because the general concept of all of them remains the same, however some of them are a lot more tedious to code out correctly because of the nature of the nested lists. (However, if you were needed to, you should still know how to code these out.) Here's what you need to do to implement this: (1) Copy the file turtle.c, call it turtle-pro.c (2) Add a new field called 'loop_head' to the CmdNode struct which is a pointer to a struct of the same type. This will be the head of the linked list of commands that are in the loop. It should be set to NULL for commands of any other type. (3) Add a function called insert CommandLoop(). This function will be used to insert a command at the end of the linked list for a 'loop' node. It will take in 2 parameters, the first will be a pointer to the loop node, and the second will be a pointer to the new command node that you have to insert into the loop. This function should not return anything, but just update the linked list at the loop node. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * NOTE: When you insert a 'loop' command into list using the interactive driver, it will then prompt you to enter all the commands that are a part of the loop one by one. Alternatively, you can write all your commands in a text file (look at box-pro.txt) and then load them in through the interactive driver. We will NOT be worrying about nested loops for now, you will learn how to handle that later in the course! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * (4) Modify the count Commands() function to now also count each command inside any loops you may have. Make sure each CmdNode is counted exactly once. For the example below, count Commands() should return 5. (5) Modify printCommandList() to print out all the commands in your loops as well. Any commands that are inside a loop should be indented by 2 spaces, for example: 0: forward 100 1: loop 4 2: forward 10 3: left 4: right (6) Modify the queryByCommand() function to now also search within the loops. You don't need to worry about indentation here. For example, a query for 'forward' in the above example should give 0: forward 100 2: forward 10 (7) Modify deleteCommandList() so that it now also frees all the nodes that are in a loop command. (8) Modify the run() command so you now also run all the commands in the loops the appropriate number of times. For instance, if you have a 'loop 4', then all the commands in the loop should be run 4 times. You can remove the insert CommandBefore(), updateCommand() and deleteCommand() functions. Feel free to implement then and leave them in if you want some more practice, however you will NOT be marked on any of them. The Part2_driver.c file contains test cases for all of the functions you will be tested on. Ensure that you see how these tests are set up so you know how your code is expected to run. FIN 48 struct cmdnode *next; } CmdNode; 49 /*- --*/ CmdNode *newCmdNode(char cmd [10], int val) { 50 51 52 53 54 55 56 57 58 59 /** * This function allocates a new CmdNode struct and initializes it's values * based on the input paramaters given. The next pointer is always * initialized to NULL. 60 * If the 'cmd' parameter is not a correct command, then print * "Invalid command. " and return NULL. 61 62 63 64 65 66 67 68 * Note that we will always pass in a value here, even if the * command doesn't need one. In this case, we can just ignore * it. It saves us from having to make separate functions to * deal with this. * TODO: Implement this function /* -*/ void printCommandList(CmdNode *head) { /** * This function prints out each command in the linked list one after the * other. Each command MUST also have a line number printed before it, this * is what you will be using to modify / delete them. To do this, initialize * a counter and then increment it for each command. 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 * Depending on whether or not the command needs an additional value * (like forward, backward and colour), use one of the following statements * to print out the information for each node: [ The format is "linenumber: command value" ] * * printf("%3d: %s %d ", ); [With a value] * printf("%3d: %s ", ... ); [Without a value] * * Obviously, you also need to pass in the correct parameters to the print * function yourself, this is just so you have the correct format. * TODO: Implement this function */ * * 147 148 149 void queryByCommand (CmdNode *head, char cmd[10]) { 150 151 * This function looks for commands in the linked list that match the input 152 * query. It prints them out in the same format as the printCommandListo) 153 * function. 154 155 * Make sure that the line number of the commands that match is the same as 156 * the line number that would be printed by the printCommandList() function. 157 158 159 160 * For instance, if your printCommandList function outputs 161 162 0: penup 163 1: forward 200 2: right 165 3: forward 50 166 167 * Then, if this function is called with the same list and cmd = "forward", 168 * then your output here should be 169 170 1: forward 200 171 3: forward 50 172 173 * TODO: Implement this function 174 */ * * 164 * *** * * 190 /*- 191 192 v int count Commands (CmdNode *head) { 193 /** 194 * This function counts and returns the length of the linked list. It 195 * requires list traversal. 196 197 * TODO: Implement this function 198 */ * 212 213 214 215 216 217 218 219 220 221 222 223 224 CmdNode *insertCommand (CmdNode *head, CmdNode *new_CmdNode) { /** * This function inserts the node new_CmdNode *at the tail* of the linked * list. (You are adding a command at the end). * * If head NULL, then the linked list is still empty. * It returns a pointer to the head of the linked list with the new node * added into it. * TODO: Implement this function */ CmdNode *insert CommandBefore(CmdNode *head, CmdNode *new_CmdNode, int cmdNum) { * This function inserts a new node *before* a given Node in the linked list. * * 'cmdNum' is an integer that corresponds to the line number of a command * from the printCommand List() function. Your task is to insert new_CmdNode * *before* the corresponding node in the linked list. * * * For instance, if your initial list was * * * * * 0: penup 1: forward 200 2: right 3: forward 50 * 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 * And you added "pendown" before cmdNum = 2, then you will have * 0: penup 1: forward 200 2: pendown 3: right 4: forward 50 * * * If there is no command with the given cmdNum (cmdNum >= list size), * then print "Invalid Command Number: " to the screen and *do not* * insert the new node. * * Returns a pointer to the head of the linked list with the new node added * into it. * * TODO: Implement this function */ 297 298 v void updateCommand (CmdNode *head, int cmdNum, char cmd[10], int val) { 299 /** 300 * This function updates a specific node in the linked list based on the 301 * input parameters. 302 303 * 'cmdNum' is an integer that corresponds to the line number of a command 304 * from the printCommand List() function. Your task is to update the 'cmd' and 305 * 'val' fields of this node. 306 307 * If there is no command with the given cmdNum, then print 308 * "Invalid Command Number. " to the screen, and if 'cmd' is not a correct 309 * command, then print "Invalid command. ". In both these cases, do *not* 310 * modify the list. 311 312 * TODO: Implement this function 313 */ * CmdNode *deleteCommand (CmdNode *head, int cmdNum) { /** * This function deletes the node from the linked list that corresponds to * the line number cmdNum. If there is no command with the given cmdNum, then * the function does nothing. 358 359 360 361 362 363 364 365 366 367 368 * * Returns a pointer to the head of the linked list (which may have changed * as a result of the deletion) * TODO: Implement this function */ CmdNode *deleteCommandList(CmdNode *head) { /** * This function deletes the linked list of commands, releasing all the * memory allocated to the nodes in the linked list. 395 396 397 398 399 400 401 402 403 404 405 * Returns a NULL pointer so that the head of the list can be set to NULL * after deletion. * TODO: Implement this function */ void run(Image *im, CmdNode *head, int *endx, int *endy) { * This function runs the list of commands to move the turtle around and draw * the image, and returns the final position of the turtle in the variables * endx and endY. * * * NOTE: In the image we work with, the top-left pixel is (0,0), - x increases as you go right, up till SIZEX-1 - y increases as you go down, up till SIZEY-1 * * * * (0,0) (SIZEX-1, 0) 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 IMAGE (0, SIZEY-1) (SIZEX-1, SIZEY-1) * The image is in grayscale (black and white), so the colours are numbers * from [0255] where 0 is black, 255 is white, and the values in between * are varying levels of gray. * * You need to understand how the (x,y) values are stored so you know how * they should be updated when you move (down means y increases, etc). You do * not need to use the 'im' variable for anything other than passing it into * the drawLine() function described below. ** * * Here's the setup - There is a turtle (with a pen) that walks along the * image. When the pen is down (on the paper), it draws a line along the path * that it walks on. (If you want to play around with this, consider looking * at the 'turtle library in python!) * * The turtle initially starts at pixel (0, 0) (at the top left], * facing right (in the positive x direction). The pen starts off * as down, with the default colour being black (0). * You need to go through the linked list and 'run' the commands to keep * track of the turtles position, and draw the appropriate lines. Here is * what each command should do: * * 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 * * penup : Put the pen up (stop drawing) - pendown : Put the pen down (start / continue drawing) colour : Draw lines in colour from now on - forward : Move the turtle forward steps (pixels) in the direction it is facing. - backward : Same as above, except move backwards - right : Turn the turtle to the right by 90 degrees - left : Turn the turtle to the left by 90 degrees * * * * NOTE: Make sure that you do *not* go outside the image. For this, set the * maximum and minimum values of x and y to be 0 and SIZEX-1 / SIZEY-1 * respectively. * * For instance, if the turtle is at (0,0) facing right, and your command is * 'forward 100000`, it will only go forward till (SIZEX-1, 0), and end * up at the rightmost pixel in that row. * * IMPORTANT: Once you are done with all the commands, make sure you save the final (x,y) location of the turtle into the variables endx and endy. * * We have already implemented a drawLine() function (in imgUtils.c) which * you should use to actually draw the lines. It takes in the following: * * a pointer to an image struct (use 'im') - (x,y) location of start point (x,y) location of end point a colour to draw the line in [0255] * 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 * Note that this function only draws horizontal and vertical lines, so * either the x values or the y values of both points must be the same. * Both these points *must* also be within the image. [ 0 cmd, cmd) == 0 && a->val == val); } int main() { Image *im; CmdNode *head = NULL; CmdNode *tmp = NULL; int val, i, j, ex, ey; printf("Running tests... "); /*---- // These should work exactly the same as the turtle.c, nothing has changed. head = insert Command (head, newCmdNode("penup", 0)); head = insert Command (head, newCmdNode("forward", 100)); head = insert Command (head, newCmdNode("right", 0)); head = insert Command (head, newCmdNode("forward", 100)); head = insert Command (head, newCmdNode("pendown", 0)); // Test , Creating a node with "loop" command tmp = newCmdNode("loop", 4); if (tmp == NULL) { printf("Test o failed, newCmdNode() did not initialize node properly. "); return 1; } else if (!checkNode(tmp, "loop", 4) || tmp->next != NULL || tmp->loop_head != NULL) { printf("Test o failed, Incorrect data in new node. "); return 1; } printf("Test o passed. "); // Test 1, Inserting elements into an empty loop-list at the tail insert CommandLoop(tmp, newCmdNode("forward, 300)); insert CommandLoop(tmp, newCmdNode("left", 0)); head = insert Command (head, tmp); if (tmp->loop_head == NULL) { printf("Test 1 failed, loop_head is still NULL. "); return 1; } else if (!checkNode(tmp->loop_head, "forward", 300) || !checkNode(tmp->loop_head->next, "left", 0) || tmp-> loop_head->next->next != NULL) { printf( "Test 1 failed, failed to insert correctly into the list for loop. "); return 1; } printf("Test 1 passed. "); 11 Test 2, Counting the number of commands in the nested lists val = count Commands (head); if (val != 8) { printf("Test 2 failed, count Commands() did not return correct value. "); // return 1; } printf("Test 2 passed. "); // Test 3, Testing the run function. Result should be identical to // test #10 from the Part 1 driver. im = newImage(SIZEX, SIZEY); run(im, head, &ex, &ey); 1/ The image should be exactly as expected, this checks every pixel to // make sure it matches. for (i = 0; i = 100 && j data[i+ j * SIZEX] != 0) { printf("Test 11 failed, Pixel at (%d, d) is %d, should be 0 ", i, j, im->data[i + j * SIZEX]); return 1; } } else if (im->data[i + j * SIZEX] != 255) { printf("Test 11 failed, Pixel at (%, %d) is %d, should be 255 ", i, j, im->data[i + j * SIZEX]); return 1; } if (ex != 100 || ey != 100) { printf("Test 3 failed, end location of the turtle should be (100, 100) "); return 1; } deleteImage(im); printf("Test 3 passed. "); // Test 4, printing out the command list printf("Test 4, check this yourself! "); * This needs to be inspected manually. Here is the expected output: 0: penup 1: forward 100 2: right 3: forward 100 4: pendown 5: loop 4 6: forward 300 // Indented by 2 spaces more than the 7: left // lines outside the loop */ print CommandList(head); // Test 5, querying the list for "forward" printf("Test 5, check this yourself! "); /* This needs to be inspected manually. Here is the expected output: 1: forward 100 3: forward 100 6: forward 300 // Note that there's no indent */ queryByCommand(head, "forward"); /*- -*/ // Test 6, Deleting the list head = deleteCommandList (head); if (head != NULL) { printf("Test 6 failed, head is not NULL after deleting list. "); return 1; } else if ((val : = countCommands (head))) { printf("Test 6 failed, count Commands() says %d commands in list. ", val); return 1; } printf("Test 6 passed. "); /* -*/ // Congratulations! your solution passed all the test cases here. This does // NOT mean it is fully correct, however. Remember to run your own tests as // well to make sure your code works under all conditions return 0; // All good } * 18 #include "imgutils.c" 19 20 // This lets the driver code override the image size if it needs to. Make sure 21 Il you don't hard-code these values anywhere! 22 #ifndef SIZEX 23 #define SIZEX 512 24 #define SIZEY 512 25 #endif 26 27 /* 28 29 30 * This struct contains one node of the linked list, which represents a single 31 * command to the Turtle. It's field should include: 32 * 33 cmd : A char array of size 10 holding the command name 34 35 - val : An integer that stores a parameter for the command (like forward, 36 backward and colour). 37 38 - next : A pointer to a struct of the same type, this is used for the 39 linked list 40 41 * TODO: Complete this struct definition 42 ****/ 43 44 typedef struct cmdnode { 45 char cmd [10]; 46 int val; 47 struct cmdnode *next; 48 } CmdNode; 49 50 * * * * * We're going to use the same data type (CmdNode) for the loop command, however we will have to add an additional field to keep track of all of the commands that are a part of the loop (in - you guessed it - another linked list!). The name for this command will simply be "loop", and the value it has will represent the number of iterations (how many times you will run all the commands in the loop). We won't quite be implementing all the functions from Part 1 here, because the general concept of all of them remains the same, however some of them are a lot more tedious to code out correctly because of the nature of the nested lists. (However, if you were needed to, you should still know how to code these out.) Here's what you need to do to implement this: (1) Copy the file turtle.c, call it turtle-pro.c (2) Add a new field called 'loop_head' to the CmdNode struct which is a pointer to a struct of the same type. This will be the head of the linked list of commands that are in the loop. It should be set to NULL for commands of any other type. (3) Add a function called insert CommandLoop(). This function will be used to insert a command at the end of the linked list for a 'loop' node. It will take in 2 parameters, the first will be a pointer to the loop node, and the second will be a pointer to the new command node that you have to insert into the loop. This function should not return anything, but just update the linked list at the loop node. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * NOTE: When you insert a 'loop' command into list using the interactive driver, it will then prompt you to enter all the commands that are a part of the loop one by one. Alternatively, you can write all your commands in a text file (look at box-pro.txt) and then load them in through the interactive driver. We will NOT be worrying about nested loops for now, you will learn how to handle that later in the course! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * (4) Modify the count Commands() function to now also count each command inside any loops you may have. Make sure each CmdNode is counted exactly once. For the example below, count Commands() should return 5. (5) Modify printCommandList() to print out all the commands in your loops as well. Any commands that are inside a loop should be indented by 2 spaces, for example: 0: forward 100 1: loop 4 2: forward 10 3: left 4: right (6) Modify the queryByCommand() function to now also search within the loops. You don't need to worry about indentation here. For example, a query for 'forward' in the above example should give 0: forward 100 2: forward 10 (7) Modify deleteCommandList() so that it now also frees all the nodes that are in a loop command. (8) Modify the run() command so you now also run all the commands in the loops the appropriate number of times. For instance, if you have a 'loop 4', then all the commands in the loop should be run 4 times. You can remove the insert CommandBefore(), updateCommand() and deleteCommand() functions. Feel free to implement then and leave them in if you want some more practice, however you will NOT be marked on any of them. The Part2_driver.c file contains test cases for all of the functions you will be tested on. Ensure that you see how these tests are set up so you know how your code is expected to run. FIN 48 struct cmdnode *next; } CmdNode; 49 /*- --*/ CmdNode *newCmdNode(char cmd [10], int val) { 50 51 52 53 54 55 56 57 58 59 /** * This function allocates a new CmdNode struct and initializes it's values * based on the input paramaters given. The next pointer is always * initialized to NULL. 60 * If the 'cmd' parameter is not a correct command, then print * "Invalid command. " and return NULL. 61 62 63 64 65 66 67 68 * Note that we will always pass in a value here, even if the * command doesn't need one. In this case, we can just ignore * it. It saves us from having to make separate functions to * deal with this. * TODO: Implement this function /* -*/ void printCommandList(CmdNode *head) { /** * This function prints out each command in the linked list one after the * other. Each command MUST also have a line number printed before it, this * is what you will be using to modify / delete them. To do this, initialize * a counter and then increment it for each command. 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 * Depending on whether or not the command needs an additional value * (like forward, backward and colour), use one of the following statements * to print out the information for each node: [ The format is "linenumber: command value" ] * * printf("%3d: %s %d ", ); [With a value] * printf("%3d: %s ", ... ); [Without a value] * * Obviously, you also need to pass in the correct parameters to the print * function yourself, this is just so you have the correct format. * TODO: Implement this function */ * * 147 148 149 void queryByCommand (CmdNode *head, char cmd[10]) { 150 151 * This function looks for commands in the linked list that match the input 152 * query. It prints them out in the same format as the printCommandListo) 153 * function. 154 155 * Make sure that the line number of the commands that match is the same as 156 * the line number that would be printed by the printCommandList() function. 157 158 159 160 * For instance, if your printCommandList function outputs 161 162 0: penup 163 1: forward 200 2: right 165 3: forward 50 166 167 * Then, if this function is called with the same list and cmd = "forward", 168 * then your output here should be 169 170 1: forward 200 171 3: forward 50 172 173 * TODO: Implement this function 174 */ * * 164 * *** * * 190 /*- 191 192 v int count Commands (CmdNode *head) { 193 /** 194 * This function counts and returns the length of the linked list. It 195 * requires list traversal. 196 197 * TODO: Implement this function 198 */ * 212 213 214 215 216 217 218 219 220 221 222 223 224 CmdNode *insertCommand (CmdNode *head, CmdNode *new_CmdNode) { /** * This function inserts the node new_CmdNode *at the tail* of the linked * list. (You are adding a command at the end). * * If head NULL, then the linked list is still empty. * It returns a pointer to the head of the linked list with the new node * added into it. * TODO: Implement this function */ CmdNode *insert CommandBefore(CmdNode *head, CmdNode *new_CmdNode, int cmdNum) { * This function inserts a new node *before* a given Node in the linked list. * * 'cmdNum' is an integer that corresponds to the line number of a command * from the printCommand List() function. Your task is to insert new_CmdNode * *before* the corresponding node in the linked list. * * * For instance, if your initial list was * * * * * 0: penup 1: forward 200 2: right 3: forward 50 * 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 * And you added "pendown" before cmdNum = 2, then you will have * 0: penup 1: forward 200 2: pendown 3: right 4: forward 50 * * * If there is no command with the given cmdNum (cmdNum >= list size), * then print "Invalid Command Number: " to the screen and *do not* * insert the new node. * * Returns a pointer to the head of the linked list with the new node added * into it. * * TODO: Implement this function */ 297 298 v void updateCommand (CmdNode *head, int cmdNum, char cmd[10], int val) { 299 /** 300 * This function updates a specific node in the linked list based on the 301 * input parameters. 302 303 * 'cmdNum' is an integer that corresponds to the line number of a command 304 * from the printCommand List() function. Your task is to update the 'cmd' and 305 * 'val' fields of this node. 306 307 * If there is no command with the given cmdNum, then print 308 * "Invalid Command Number. " to the screen, and if 'cmd' is not a correct 309 * command, then print "Invalid command. ". In both these cases, do *not* 310 * modify the list. 311 312 * TODO: Implement this function 313 */ * CmdNode *deleteCommand (CmdNode *head, int cmdNum) { /** * This function deletes the node from the linked list that corresponds to * the line number cmdNum. If there is no command with the given cmdNum, then * the function does nothing. 358 359 360 361 362 363 364 365 366 367 368 * * Returns a pointer to the head of the linked list (which may have changed * as a result of the deletion) * TODO: Implement this function */ CmdNode *deleteCommandList(CmdNode *head) { /** * This function deletes the linked list of commands, releasing all the * memory allocated to the nodes in the linked list. 395 396 397 398 399 400 401 402 403 404 405 * Returns a NULL pointer so that the head of the list can be set to NULL * after deletion. * TODO: Implement this function */ void run(Image *im, CmdNode *head, int *endx, int *endy) { * This function runs the list of commands to move the turtle around and draw * the image, and returns the final position of the turtle in the variables * endx and endY. * * * NOTE: In the image we work with, the top-left pixel is (0,0), - x increases as you go right, up till SIZEX-1 - y increases as you go down, up till SIZEY-1 * * * * (0,0) (SIZEX-1, 0) 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 IMAGE (0, SIZEY-1) (SIZEX-1, SIZEY-1) * The image is in grayscale (black and white), so the colours are numbers * from [0255] where 0 is black, 255 is white, and the values in between * are varying levels of gray. * * You need to understand how the (x,y) values are stored so you know how * they should be updated when you move (down means y increases, etc). You do * not need to use the 'im' variable for anything other than passing it into * the drawLine() function described below. ** * * Here's the setup - There is a turtle (with a pen) that walks along the * image. When the pen is down (on the paper), it draws a line along the path * that it walks on. (If you want to play around with this, consider looking * at the 'turtle library in python!) * * The turtle initially starts at pixel (0, 0) (at the top left], * facing right (in the positive x direction). The pen starts off * as down, with the default colour being black (0). * You need to go through the linked list and 'run' the commands to keep * track of the turtles position, and draw the appropriate lines. Here is * what each command should do: * * 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 * * penup : Put the pen up (stop drawing) - pendown : Put the pen down (start / continue drawing) colour : Draw lines in colour from now on - forward : Move the turtle forward steps (pixels) in the direction it is facing. - backward : Same as above, except move backwards - right : Turn the turtle to the right by 90 degrees - left : Turn the turtle to the left by 90 degrees * * * * NOTE: Make sure that you do *not* go outside the image. For this, set the * maximum and minimum values of x and y to be 0 and SIZEX-1 / SIZEY-1 * respectively. * * For instance, if the turtle is at (0,0) facing right, and your command is * 'forward 100000`, it will only go forward till (SIZEX-1, 0), and end * up at the rightmost pixel in that row. * * IMPORTANT: Once you are done with all the commands, make sure you save the final (x,y) location of the turtle into the variables endx and endy. * * We have already implemented a drawLine() function (in imgUtils.c) which * you should use to actually draw the lines. It takes in the following: * * a pointer to an image struct (use 'im') - (x,y) location of start point (x,y) location of end point a colour to draw the line in [0255] * 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 * Note that this function only draws horizontal and vertical lines, so * either the x values or the y values of both points must be the same. * Both these points *must* also be within the image. [ 0 cmd, cmd) == 0 && a->val == val); } int main() { Image *im; CmdNode *head = NULL; CmdNode *tmp = NULL; int val, i, j, ex, ey; printf("Running tests... "); /*---- // These should work exactly the same as the turtle.c, nothing has changed. head = insert Command (head, newCmdNode("penup", 0)); head = insert Command (head, newCmdNode("forward", 100)); head = insert Command (head, newCmdNode("right", 0)); head = insert Command (head, newCmdNode("forward", 100)); head = insert Command (head, newCmdNode("pendown", 0)); // Test , Creating a node with "loop" command tmp = newCmdNode("loop", 4); if (tmp == NULL) { printf("Test o failed, newCmdNode() did not initialize node properly. "); return 1; } else if (!checkNode(tmp, "loop", 4) || tmp->next != NULL || tmp->loop_head != NULL) { printf("Test o failed, Incorrect data in new node. "); return 1; } printf("Test o passed. "); // Test 1, Inserting elements into an empty loop-list at the tail insert CommandLoop(tmp, newCmdNode("forward, 300)); insert CommandLoop(tmp, newCmdNode("left", 0)); head = insert Command (head, tmp); if (tmp->loop_head == NULL) { printf("Test 1 failed, loop_head is still NULL. "); return 1; } else if (!checkNode(tmp->loop_head, "forward", 300) || !checkNode(tmp->loop_head->next, "left", 0) || tmp-> loop_head->next->next != NULL) { printf( "Test 1 failed, failed to insert correctly into the list for loop. "); return 1; } printf("Test 1 passed. "); 11 Test 2, Counting the number of commands in the nested lists val = count Commands (head); if (val != 8) { printf("Test 2 failed, count Commands() did not return correct value. "); // return 1; } printf("Test 2 passed. "); // Test 3, Testing the run function. Result should be identical to // test #10 from the Part 1 driver. im = newImage(SIZEX, SIZEY); run(im, head, &ex, &ey); 1/ The image should be exactly as expected, this checks every pixel to // make sure it matches. for (i = 0; i = 100 && j data[i+ j * SIZEX] != 0) { printf("Test 11 failed, Pixel at (%d, d) is %d, should be 0 ", i, j, im->data[i + j * SIZEX]); return 1; } } else if (im->data[i + j * SIZEX] != 255) { printf("Test 11 failed, Pixel at (%, %d) is %d, should be 255 ", i, j, im->data[i + j * SIZEX]); return 1; } if (ex != 100 || ey != 100) { printf("Test 3 failed, end location of the turtle should be (100, 100) "); return 1; } deleteImage(im); printf("Test 3 passed. "); // Test 4, printing out the command list printf("Test 4, check this yourself! "); * This needs to be inspected manually. Here is the expected output: 0: penup 1: forward 100 2: right 3: forward 100 4: pendown 5: loop 4 6: forward 300 // Indented by 2 spaces more than the 7: left // lines outside the loop */ print CommandList(head); // Test 5, querying the list for "forward" printf("Test 5, check this yourself! "); /* This needs to be inspected manually. Here is the expected output: 1: forward 100 3: forward 100 6: forward 300 // Note that there's no indent */ queryByCommand(head, "forward"); /*- -*/ // Test 6, Deleting the list head = deleteCommandList (head); if (head != NULL) { printf("Test 6 failed, head is not NULL after deleting list. "); return 1; } else if ((val : = countCommands (head))) { printf("Test 6 failed, count Commands() says %d commands in list. ", val); return 1; } printf("Test 6 passed. "); /* -*/ // Congratulations! your solution passed all the test cases here. This does // NOT mean it is fully correct, however. Remember to run your own tests as // well to make sure your code works under all conditions return 0; // All good }

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

Data Mining Concepts And Techniques

Authors: Jiawei Han, Micheline Kamber, Jian Pei

3rd Edition

0123814790, 9780123814791

More Books

Students also viewed these Databases questions

Question

List out some inventory management techniques.

Answered: 1 week ago