Question
#include #define MAX50 int position_row, position_column, direction, penDown, walk; intis_valid_command(intcommand){ if(command>0&& command up int command, command1; position_row=0; position_column=0; direction=1; penDown=0; walk=0; //Initialize floor with zeros
#include
#define MAX50
int position_row, position_column, direction, penDown, walk;
intis_valid_command(intcommand){
if(command>0&& command<=9)
return1;
return0;
}
voidprintFloor(int floor[][MAX]){
for(int i=0; i for(int j=0; j if(floor[i][j]==1)printf("*"); elseprintf(" "); } printf(" "); } } voidexecute_command(intcommand,int floor[][MAX]){ if(!walk){ if(command==1){ penDown=1; } elseif(command==2){ penDown=0; } elseif(command==3){ if(direction>1) direction--; else direction=4; } elseif(command==4){ if(direction<4) direction++; else direction=1; } elseif(command==6){ printFloor(floor); } } else { if(direction==1){ if(position_column+ command< MAX){ for(int i=0; i<= command; i++){ if(penDown)floor[position_row][position_column+ i]=1; } position_column+= command; } } elseif(direction==2){ if(position_row+ command< MAX){ for(int i=0; i<= command; i++){ if(penDown)floor[position_row+ i][position_column]=1; } position_row+= command; } } elseif(direction==3){ if(position_column- command>=0){ for(int i=0; i<= command; i++){ if(penDown)floor[position_row][position_column- i]=1; } position_column-= command; } } else{ if(position_row- command>=0){ for(int i=0; i<= command; i++){ if(penDown)floor[position_row-i][position_column]=1; } position_row-= command; } } } } intmain() { int rows, columns; intfloor[MAX][MAX]; //direction: 1-> right, 2-> down, 3-> left, 4->up int command, command1; position_row=0; position_column=0; direction=1; penDown=0; walk=0; //Initialize floor with zeros for (rows=0; rows< MAX; rows++) for (columns=0; columns< MAX; columns++) floor[rows][columns]=0; //Keeps on running until command 9 is entered do{ scanf("%d",&command); if (command==5) { scanf(",%d",&command1); walk=1; } else{ walk=0; } if(is_valid_command(command)&& walk) execute_command(command1,floor); elseif(is_valid_command(command)&&!walk) execute_command(command,floor); } while(command!=9); return0; } A mechanical turtle that walks around the room under the control of a C program. The turtle holds a pen in one of two positions, up or down. While the pen is down, the turtle traces out shapes as it moves; while the pen is up, the turtle moves about freely without writing anything. In this problem, you'll simulate the operation of the turtle and create computerized sketchpad. Use a 50-by-50 arrayfloorwhich is initialized to zeros. Read commands from an array that contains them. Keep track of the current turtle position at all times and whether the pen is currently up or down. Assume that the turtle always starts at position 0, 0 of the floor with its pen up, facing right. The set of turtle commands your program must process is shown below. For example, the following "turtle program" would draw and print a 12-by-12 square 1 5,12 4 5,12 4 5,12 4 5,12 2 6 9 As the turtle moves with the pen down, set the appropriate elements of the array floor to 1s. When the command 6 (print) is given, wherever there's a 1 in the array, display an asterisk, or some other character you choose. Wherever there's a zero, display a blank. We assume that the floor has 4 walls. Thus, any attempt to go beyond the floor will make the turtle only reach the boundary of the floor. The array to hold all commands is necessary.One of the benefits is that when you send in command 6, the floor array won't be displayed immediately. The whole turtle program will be executed when command 9 is entered. Command 6 can appear more than once. Without the command array, commands are executed interactively and the turtle programs can hardly be called programs. You may write various "turtle programs' to test the program. Turtle commands: Command Meaning 1 Pen down 2 Pen up 3 Turn left 4 Turn right 5, N Move forward N spaces (N is a number) 6 Print the 50-by-50 arrayfloor 9 End of data (sentinel) 1. (12 marks)Look at the C program that solved the above problem, but it does not meet the above problem requirements in two major areas. This C program A2_flawed.c is provided. Please check this code and point out its two major problems. 2. (8 marks)Modify A2_flawed.c and fix the two major problems you have found. Do not write completely new program to solve the problem, only half of the marks will be given.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started