Question: in the function get_command_line_params use memcpy() and memcmp() Function get_command_line_params should have a return value of void and parameters of argc and argv { Create
in the function get_command_line_params use memcpy() and memcmp()
Function get_command_line_params should have a return value of void and parameters of argc and argv
{
Create a for loop with argc to go through each value of argv Look for an argv of DIRECTIONS= When found, take the string after the = as the value to store in DirectionsFilename. Look for an argv of MAP= When found, take the string after the = as the value to store in MapFilename. After looking at all of the strings in argv, if you do not have a value for either DirectionsFilename or MapFilename, then print the message "DIRECTIONS= and MAP= must be given on the command line" and exit
} main() { Declare two variables of type FILE * called DirectionFile and TreasureMap Create a char array called DirectionList Create a 2D char array called Map. The array should be MAPSIZE for both dimensions (it is square). Create a char array named buffer of size 2. Create int variables i, j and k. These will be used later in for loops. Declare a variable called Position of type struct RowCol. Declare a pointer to Position called PositionPtr and initialize it to the address of Position. Call function get_command_line_params Pass DirectionsFilename to fopen() with a mode of r+ and store the return value in DirectionFile. Check if DirectionFile is NULL. If it is, call perror() with "DirectionFile did not open" and exit. Pass MapFilename to fopen() with a mode of r+ and store the return value in TreasureMap. Check if TreasureMap is NULL. If it is, call perror() with "TreasureMap did not open" and exit. Use fgets() to read 500 characters from DirectionFile into DirectionList /* Initialize the map to dashes */ Create a for loop from j = 0 to j < MAPSIZE
{
Create a for loop from k = 0 to k < MAPSIZE (this is nested inside the j for loop)
{
Set Map[j][k] to a dash
}
}
Set Position.row to 0 Set Position.col to 0 Set Map[Position.row][Position.col] to 'S' to signal the starting position Create a for loop from i = 0 to i < the string length of DirectionList
{
if DirectionList[i] is 'N', then call MoveNorth() and pass PositionPtr
else if DirectionList[i] is 'S', then call MoveSouth() and pass PositionPtr
else if DirectionList[i] is 'W', then call MoveWest() and pass PositionPtr
else call MoveEast() and pass PositionPtr
Set Map[Position.row][Position.col] to 'X' to signal a move to this position
} Set the last Map point (Map[Position.row][Position.col]) to 'E' to signal the ending position Create a for loop from j = 0 to j < MAPSIZE
{
Create a for loop from k = 0 to k < MAPSIZE (this is nested inside the j for loop) {
Use "%c " to print Map[j][k].
Use sprintf() to print Map[j][k] to buffer.
Use fputs() to write buffer to TreasureMap (the file TreasureMap will contain NO spaces and NO linefeeds)
}
Use printf() to print two blanks lines this will put blank lines between each row
}
Call fclose() with DirectionFile to close it. Call fclose() with TreasureMap to close it }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
