n the function get_command_line_params it needs to include memcpy() and memcmp()
ID: 3715438 • Letter: N
Question
n the function get_command_line_params it needs to include memcpy() and memcmp()
Create a #define called MAPSIZE. Set to 10.
Create global variables DirectionsFilename and MapFilename. Use char arrays and set them to nulls by using {}.
Create a structure called RowCol that has two int members called row and col to hold the row and column.
Function MoveNorth should have a return value of void and a parameter of a pointer to RowCol.
{
To move north, you need to subtract 1 from PositionPtr->row. Before doing so, you need to check if subtracting one makes you move outside of the map – if subtracting 1 makes PositionPtr->row < 0, then you have moved outside of the map; therefore, the move is not valid and should be skipped.
}
Function MoveSouth should have a return value of void and a parameter of a pointer to RowCol.
{
To move south, you need to add 1 to PositionPtr->row. Before doing so, you need to check if adding one makes you move outside of the map – if adding 1 makes PositionPtr->row > 10, then you have moved outside of the map; therefore, the move is not valid and should be skipped.
}
Function MoveEast should have a return value of void and a parameter of a pointer to RowCol.
{
To move east, you need to add 1 to PositionPtr->col. Before doing so, you need to check if adding one makes you move outside of the map – if adding 1 makes PositionPtr->col > 10, then you have moved outside of the map; therefore, the move is not valid and should be skipped.
}
Function MoveWest should have a return value of void and a parameter of a pointer to RowCol.
{
To move west, you need to subtract 1 from PositionPtr->col. Before doing so, you need to check if subtracting one makes you move outside of the map – if subtracting 1 makes PositionPtr->col < 0, then you have moved outside of the map; therefore, the move is not valid and should be skipped.
}
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
}
Explanation / Answer
#define MAPSIZE 10
char DirectionFilename[] = {0};
Char MapFilename[] = {0};
struct {
Int row;
Int col;
} RowCol;
void MoveNorth(RowCol* PositionPtr)
{
if ((PositionPtr->row - 1) >= 0)
{
PositionPtr->row = PositionPtr->row - 1;
}
}
void MoveSouth(RowCol* PositionPtr)
{
if ((PositionPtr->row + 1) <= 9)
{
PositionPtr->row = PositionPtr->row + 1;
}
}
void MoveEast(RowCol* PositionPtr)
{
if ((PositionPtr->col + 1) <= 9)
{
PositionPtr->col = PositionPtr->col + 1;
}
}
void MoveWest(RowCol* PositionPtr)
{
if ((PositionPtr->col - 1) >= 0)
{
PositionPtr->col = PositionPtr->col - 1;
}
}
void get_command_line_params(int argc, char *argv[])
{
char flag = 0;
Int count = 0;
char *ptr;
for (int loop = 0;loop < argc;loop++)
{
if ((memcmp(argv[loop], “DIRECTIONS=”) == 0)
{
count = 0;
ptr = argv[argc];
while (*ptr != ‘’)
{
count++;
ptr++;
}
memcpy(DirectionFilename, *ptr, count);
flag = 1;
}
else if ((memcmp(argv[loop], “MAP=”) == 0)
{
count = 0;
ptr = argv[argc];
while (*ptr != ‘’)
{
count++;
ptr++;
}
memcpy(MapFilename, *ptr, count);
flag = 1;
}
}
If (flag == 0)
{
printf(“DIRECTIONS= and MAP= must be given on the command line.”);
}
}
Int main()
{
File *DirectionFile, *TreasureMap;
char DirectionList[] = {0};
char Map[MAPSIZE][MAPSIZE];
char buffer[2];
int i,j,k;
struct RowCol Position;
struct RowCol*PositionPtr = &Position;
get_command_line_params();
DirectionFile = fopen(DirectionFilename, “r+”);
if (DirectionFile == NUL)
{
perror(“DirectionFile did not open”);
exit();
}
TreasureFile = fopen(MapFilename, “r+”);
if (TreasureFile == NUL)
{
perror(“TreasureMap did not open”);
exit();
}
i =0;
While (i <= 500)
{
I++;
DirectionList = fgets(DirectionFile);
}
for (j = 0;j < MAPSIZE; j++)
{
for (k =0; k< MAPSIZE;k++)
{
Map[j][k] = dash;
}
}
Position.row = 0;
Position.col = 0;
Map[Position.row][Position.col] = ’S’;
for (i = 0;i < DirectionList;i++)
{
If (DirectionList[i] = ’N’)
{
MoveNorth(PositionPtr);
}
Else If (DirectionList[i] = ’S’)
{
MoveSouth(PositionPtr);
}
If (DirectionList[i] = ’W’)
{
MoveWest(PositionPtr);
}
Else
{
MoveEast(PositionPtr);
}
Map[Position.row][Position.col] = ‘E’;
}
For (j=0;j< MAPSIZE;j++)
{
For (k=0;k<MAPSIZE;k++)
{
printf(“%c”, Map[j][k];
sprintf(buffer, “%s”, Map[j][k]);
fputs(buffer, TreasureMap);
}
printf(" ");
fclose(DirecitionFile);
fclose(TreasureMap);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.