in the function get_command_line_params it needs to include memcpy() and memcmp(
ID: 3715207 • Letter: I
Question
in the function get_command_line_params it needs to include 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
}
Explanation / Answer
In the asked question, few things are unclear like what would be the map size and what MoveNorth(PositionPtr), MoveSouth(PositionPtr), etc function does. So I have assumed few things and kept the function definitions empty so that you can write the desired code in it as I am unable to figure it out. Still no worries, if you want that also implemented, please comment and I will certainly try to answer to your queries or I will give the code for those functions also. Below code gives the steps as mentioned by you.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstdlib>
#define MAX 10000
#define MAPSIZE 100
struct RowCol
{
int row;
int col;
};
char DirectionsFilename[MAX], MapFilename[MAX];
void get_command_line_params(int argc, char **argv)
{
int directionStringLength = 11;
int mapStringLength = 4;
int j;
for(int i = 1; i < argc; i++)
{
char dirString[MAX];
for(j = 0; j < directionStringLength; j++)
dirString[j] = argv[i][j];
dirString[j] = '';
char mapString[MAX];
for(j = 0; j < mapStringLength; j++)
mapString[j] = argv[i][j];
mapString[j] = '';
if(memcmp("DIRECTIONS=", dirString, directionStringLength) == 0)
{
// Get the string after directions
char dirName[MAX];
for(j = directionStringLength; j < strlen(argv[i]); j++)
dirName[j] = argv[i][j];
dirName[j] = '';
memcpy(DirectionsFilename, dirName, strlen(argv[i]) - directionStringLength);
}
else if(memcmp("MAP=", mapString, mapStringLength) == 0)
{
// Get the string after directions
char mapName[MAX];
for(j = mapStringLength; j < strlen(argv[i]); j++)
mapName[j] = argv[i][j];
mapName[j] = '';
memcpy(MapFilename, mapName, strlen(argv[i]) - mapStringLength);
}
else
{
perror("DIRECTIONS= and MAP= must be given on the command line");
}
}
}
int main(int argc, char **argv)
{
// Here the primary check would to count the number of command line arguments, total it needs to be 3;
// because the syntax would be "mycode.out DIRECTIONS=dir MAP=map"
if(argc != 3)
{
perror("Please enter the required command line arguments. The syntax of running the program is mycode.c DIRECTIONS=dir MAP=map");
return(-1);
}
FILE *DirectionFile = NULL, *TreasureMap = NULL;
char DirectionList[MAX];
char Map[MAPSIZE][MAPSIZE];
char buffer[2];
int i, j, k;
struct RowCol Position;
struct RowCol *PositionPtr = &Position;
get_command_line_params(argc, argv);
DirectionFile = fopen(DirectionsFilename, "r+");
if(DirectionFile == NULL)
{
perror("DirectionFile did not open");
return(-1);
}
TreasureMap = fopen(MapFilename, "r+");
if(TreasureMap == NULL)
{
perror("TreasureMap did not open");
return(-1);
}
fgets (DirectionList, 500, DirectionFile);
for(j = 0; j < MAPSIZE; j++)
{
for(k = 0; k < MAPSIZE; k++)
{
Map[j][k] = '-';
}
}
Position.row = 0;
Position.col = 0;
Map[Position.row][Position.col] = 'S';
for(i = 0; i < strlen(DirectionList); i++)
{
if(DirectionList[i] == 'N')
MoveNorth(PositionPtr);
else if(DirectionList[i] == 'S')
MoveSouth(PositionPtr);
else if(DirectionList[i] == 'W')
MoveWest(PositionPtr);
else
MoveEast(PositionPtr);
Map[Position.row][Position.col] = 'X';
}
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, "%c" , Map[j][k]);
fputs(buffer, TreasureMap);
}
printf(" ");
}
fclose(DirectionFile);
fclose(TreasureMap);
return 0;
}
In case of any question, you can comment below.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstdlib>
#define MAX 10000
#define MAPSIZE 100
struct RowCol
{
int row;
int col;
};
char DirectionsFilename[MAX], MapFilename[MAX];
void get_command_line_params(int argc, char **argv)
{
int directionStringLength = 11;
int mapStringLength = 4;
int j;
for(int i = 1; i < argc; i++)
{
char dirString[MAX];
for(j = 0; j < directionStringLength; j++)
dirString[j] = argv[i][j];
dirString[j] = '';
char mapString[MAX];
for(j = 0; j < mapStringLength; j++)
mapString[j] = argv[i][j];
mapString[j] = '';
if(memcmp("DIRECTIONS=", dirString, directionStringLength) == 0)
{
// Get the string after directions
char dirName[MAX];
for(j = directionStringLength; j < strlen(argv[i]); j++)
dirName[j] = argv[i][j];
dirName[j] = '';
memcpy(DirectionsFilename, dirName, strlen(argv[i]) - directionStringLength);
}
else if(memcmp("MAP=", mapString, mapStringLength) == 0)
{
// Get the string after directions
char mapName[MAX];
for(j = mapStringLength; j < strlen(argv[i]); j++)
mapName[j] = argv[i][j];
mapName[j] = '';
memcpy(MapFilename, mapName, strlen(argv[i]) - mapStringLength);
}
else
{
perror("DIRECTIONS= and MAP= must be given on the command line");
}
}
}
int main(int argc, char **argv)
{
// Here the primary check would to count the number of command line arguments, total it needs to be 3;
// because the syntax would be "mycode.out DIRECTIONS=dir MAP=map"
if(argc != 3)
{
perror("Please enter the required command line arguments. The syntax of running the program is mycode.c DIRECTIONS=dir MAP=map");
return(-1);
}
FILE *DirectionFile = NULL, *TreasureMap = NULL;
char DirectionList[MAX];
char Map[MAPSIZE][MAPSIZE];
char buffer[2];
int i, j, k;
struct RowCol Position;
struct RowCol *PositionPtr = &Position;
get_command_line_params(argc, argv);
DirectionFile = fopen(DirectionsFilename, "r+");
if(DirectionFile == NULL)
{
perror("DirectionFile did not open");
return(-1);
}
TreasureMap = fopen(MapFilename, "r+");
if(TreasureMap == NULL)
{
perror("TreasureMap did not open");
return(-1);
}
fgets (DirectionList, 500, DirectionFile);
for(j = 0; j < MAPSIZE; j++)
{
for(k = 0; k < MAPSIZE; k++)
{
Map[j][k] = '-';
}
}
Position.row = 0;
Position.col = 0;
Map[Position.row][Position.col] = 'S';
for(i = 0; i < strlen(DirectionList); i++)
{
if(DirectionList[i] == 'N')
MoveNorth(PositionPtr);
else if(DirectionList[i] == 'S')
MoveSouth(PositionPtr);
else if(DirectionList[i] == 'W')
MoveWest(PositionPtr);
else
MoveEast(PositionPtr);
Map[Position.row][Position.col] = 'X';
}
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, "%c" , Map[j][k]);
fputs(buffer, TreasureMap);
}
printf(" ");
}
fclose(DirectionFile);
fclose(TreasureMap);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.