Create a #define called MAPSIZE. Set to 10. Create global variables DirectionsFi
ID: 3712968 • Letter: C
Question
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
MAIN.CPP
#include<string>
#include <vector>
#include <iostream>
#include <iomanip>
#include <direct.h>
#include <cstdio>
using namespace std;
//Create a #define called MAPSIZE. Set to 10.
#define MAPSIZE 10
//Create global variables DirectionsFilename and MapFilename. Use char arrays and set them to nulls by using {}.
char DirectionsFilename[100] = { NULL };
char MapFilename[100] ={ NULL };
//Create a structure called RowCol that has two int members called row and col to hold the row and column.
struct RowCol
{
int Row;
int Col;
};
//Function MoveNorth should have a return value of void and a parameter of a pointer to RowCol.
void MoveNorth(RowCol *PositionPtr);
void MoveSouth(RowCol *PositionPtr);
void MoveEast(RowCol *PositionPtr);
void MoveWest(RowCol *PositionPtr);
//Function get_command_line_params should have a return value of void and parameters of argc and argv
void get_command_line_params(int argc, std::vector<std::string> argv);
/* additional function to see map as it develops*/
void displaycurrentMoveInMAP(char cdirection, char dMap[MAPSIZE][MAPSIZE]);
char findDirectionFileString[] = "DIRECTIONS=";
char findMAPFileString[] = "MAP=";
int main(int argc, char **argv)
{
//Declare two variables of type FILE * called DirectionFile and TreasureMap
FILE *DirectionFile, *TreasureMap;
//Create a char array called DirectionList
char *DirectionList = { '' };
//Create a 2D char array called Map. The array should be MAPSIZE for both dimensions (it is square).
char Map[MAPSIZE][MAPSIZE] = {''} ;
//Create a char array named buffer of size 2.
char buffer[2] = {''};
//Create int variables i, j and k. These will be used later in for loops.
int i,j,k;
//Declare a variable called Position of type struct RowCol.
RowCol Position;
//Declare a pointer to Position called PositionPtr and initialize it to the address of Position.
RowCol *PositionPtr = &Position;
//Call function get_command_line_params
std::vector<std::string> args(argv + 0, argv + argc);
get_command_line_params(argc,args);
//Pass DirectionsFilename to fopen() with a mode of r+ and store the return value in DirectionFile.
DirectionFile = fopen(DirectionsFilename , "r");
//Check if DirectionFile is NULL. If it is, call perror() with "DirectionFile did not open" and exit.
if (DirectionFile == NULL)
{
perror("DirectionFile did not open" );
exit(0);
}
//Pass MapFilename to fopen() with a mode of r+ and store the return value in TreasureMap.
TreasureMap = fopen(MapFilename , "w+");
//Check if TreasureMap is NULL. If it is, call perror() with "TreasureMap did not open" and exit.
if (TreasureMap == NULL)
{
perror("TreasureMap did not open" );
exit(0);
}
////Use fgets() to read 500 characters from DirectionFile into DirectionList
DirectionList = (char *)malloc(500);
fgets (DirectionList, 500 , DirectionFile ) ;
/* Initialize the map to dashes */
//Create a for loop from j = 0 to j < MAPSIZE
for (j=0;j<MAPSIZE;j++)
{
//Create a for loop from k = 0 to k < MAPSIZE (this is nested inside the j for loop)
for (k=0;k<MAPSIZE;k++)
{
//Set Map[j][k] to a dash
Map[j][k] = '-';
}
}
//Set Position.row to 0
Position.Row =0;
//Set Position.col to 0
Position.Col = 0;
//Set Map[Position.row][Position.col] to 'S' to signal the starting position
Map[Position.Row][Position.Col] = 'S';
displaycurrentMoveInMAP(' ', Map);
//Create a for loop from i = 0 to i < the string length of DirectionList
int len= strlen(DirectionList);
for(i=0;i<len;i++)
{
//if DirectionList[i] is 'N', then call MoveNorth() and pass PositionPtr
if (DirectionList[i] == 'N')
MoveNorth(PositionPtr);
//else if DirectionList[i] is 'S', then call MoveSouth() and pass PositionPtr
else if (DirectionList[i] == 'S')
MoveSouth(PositionPtr);
//else if DirectionList[i] is 'W', then call MoveWest() and pass PositionPtr
else if (DirectionList[i] == 'W')
MoveWest(PositionPtr);
//else call MoveEast() and pass PositionPtr
else if (DirectionList[i] == 'E')
MoveEast(PositionPtr);
//Set Map[Position.row][Position.col] to 'X' to signal a move to this position
Map[Position.Row][Position.Col] = 'X' ;
displaycurrentMoveInMAP(DirectionList[i], Map);
}
//Set the last Map point (Map[Position.row][Position.col]) to 'E' to signal the ending position
Map[Position.Row][Position.Col] = 'E';
displaycurrentMoveInMAP(' ', Map);
//Create a for loop from j = 0 to j < MAPSIZE
for(j=0;j<MAPSIZE;j++)
{
//Create a for loop from k = 0 to k < MAPSIZE (this is nested inside the j for loop)
for(k=0;k<MAPSIZE;k++)
{
//Use "%c " to print Map[j][k].
printf("%c ",Map[j][k]);
//Use sprintf() to print Map[j][k] to buffer.
sprintf(buffer, "%c",Map[j][k]);
//Use fputs() to write buffer to TreasureMap (the file TreasureMap will contain NO spaces and NO linefeeds)
fputs (buffer,TreasureMap);
}
//Use printf() to print two blanks lines – this will put blank lines between each row
printf(" ");
}
system("pause");
//Call fclose() with DirectionFile to close it.
fclose(DirectionFile);
//Call fclose() with TreasureMap to close it
fclose(TreasureMap);
}
void MoveNorth(RowCol *PositionPtr)
{
// 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.
if (PositionPtr->Row - 1 >= 0)
PositionPtr->Row = PositionPtr->Row - 1;
}
void MoveSouth(RowCol *PositionPtr)
{
//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.;
if (PositionPtr->Row + 1 < MAPSIZE )
PositionPtr->Row = PositionPtr->Row + 1;
}
void MoveEast(RowCol *PositionPtr)
{
//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.
if (PositionPtr->Col + 1 < MAPSIZE )
PositionPtr->Col = PositionPtr->Col + 1;
}
void MoveWest(RowCol *PositionPtr)
{
//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.
if (PositionPtr->Col - 1 >= 0)
PositionPtr->Col = PositionPtr->Col - 1;
}
void get_command_line_params(int argc, std::vector<std::string> argv)
{
std::string args;
bool bFoundDirectionsFile = false;
bool bFoundMapFile = false;
std::size_t foundat ;
std::string str;
//Create a for loop with argc to go through each value of argv
for(int a=0;a<argc;a++)
{
//Look for an argv of DIRECTIONS=
//When found, take the string after the = as the value to store in
//DirectionsFilename.
args = argv[a];
foundat = args.find(findDirectionFileString);
if (foundat!=std::string::npos && bFoundDirectionsFile == false)
{
// copying the contents of the string to char array
//strcpy(DirectionsFilename, args.substr(foundat).c_str());
str = args.substr(foundat+strlen(findDirectionFileString)).c_str();
str.copy(DirectionsFilename, str.length());
bFoundDirectionsFile = true;
}
//Look for an argv of MAP=
//When found, take the string after the = as the value to store
//in MapFilename.
foundat = args.find(findMAPFileString);
if (foundat!=std::string::npos && bFoundMapFile == false)
{
//copying the contents of the string to char array
//strcpy(MapFilename, args.substr(foundat).c_str());
str = args.substr(foundat+strlen(findMAPFileString)).c_str();
str.copy(MapFilename, str.length());
bFoundMapFile = true;
}
}
//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
if (!bFoundDirectionsFile || !bFoundMapFile)
{
printf("DIRECTIONS= and MAP= must be given on the command line");
exit(0);
}
}
void displaycurrentMoveInMAP(char cdirection, char dMap[MAPSIZE][MAPSIZE])
{
string sdirection;
if (cdirection == 'E')
sdirection = "EAST DIRECTION";
else if (cdirection == 'W')
sdirection = "WEST DIRECTION";
else if (cdirection == 'N')
sdirection = "NORTH DIRECTION";
else if (cdirection == 'S')
sdirection = "SOUTH DIRECTION";
else
sdirection = "START OR END POSITION";
cout << " CURRENT MOVE IN " << sdirection << " ";
int j,k;
//Create a for loop from j = 0 to j < MAPSIZE
for(j=0;j<MAPSIZE;j++)
{
//Create a for loop from k = 0 to k < MAPSIZE (this is nested inside the j for loop)
for(k=0;k<MAPSIZE;k++)
{
//Use "%c " to print Map[j][k].
printf("%c ",dMap[j][k]);
}
//Use printf() to print two blanks lines – this will put blank lines between each row
printf(" ");
}
system("pause");
}
OUTPUT : WRONG COMMAND LINE
D:UsersjdsDocumentsVisual Studio 2010ProjectsWriteFileTestDebug>WriteFile
Test
DIRECTIONS= and MAP= must be given on the command line
D:UsersjdsDocumentsVisual Studio 2010ProjectsWriteFileTestDebug>WriteFile
Test d m
DIRECTIONS= and MAP= must be given on the command line
OUTPUT : WRONG FILE NAME
D:UsersjdsDocumentsVisual Studio 2010ProjectsWriteFileTestDebug>WriteFile
Test DIRECTIONS=directionsnotfound.txt MAP=map.txt
DirectionFile did not open: No such file or directory
INPUT FILE : EXECUTION WITH CORRECT DIRECTIONS.TXT FILE
NNSSEESSWW
OUTPUT FILE : EXECUTION WITH CORRECT DIRECTIONS.TXT FILE
D:UsersjdsDocumentsVisual Studio 2010ProjectsWriteFileTestDebug>WriteFile
Test DIRECTIONS=directions.txt MAP=map.txt
CURRENT MOVE IN START OR END POSITION
S - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
Press any key to continue . . .
CURRENT MOVE IN NORTH DIRECTION
X - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
Press any key to continue . . .
CURRENT MOVE IN NORTH DIRECTION
X - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
Press any key to continue . . .
CURRENT MOVE IN SOUTH DIRECTION
X - - - - - - - - -
X - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
Press any key to continue . . .
CURRENT MOVE IN SOUTH DIRECTION
X - - - - - - - - -
X - - - - - - - - -
X - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
Press any key to continue . . .
CURRENT MOVE IN EAST DIRECTION
X - - - - - - - - -
X - - - - - - - - -
X X - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
Press any key to continue . . .
CURRENT MOVE IN EAST DIRECTION
X - - - - - - - - -
X - - - - - - - - -
X X X - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
Press any key to continue . . .
CURRENT MOVE IN SOUTH DIRECTION
X - - - - - - - - -
X - - - - - - - - -
X X X - - - - - - -
- - X - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
Press any key to continue . . .
CURRENT MOVE IN SOUTH DIRECTION
X - - - - - - - - -
X - - - - - - - - -
X X X - - - - - - -
- - X - - - - - - -
- - X - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
Press any key to continue . . .
CURRENT MOVE IN WEST DIRECTION
X - - - - - - - - -
X - - - - - - - - -
X X X - - - - - - -
- - X - - - - - - -
- X X - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
Press any key to continue . . .
CURRENT MOVE IN WEST DIRECTION
X - - - - - - - - -
X - - - - - - - - -
X X X - - - - - - -
- - X - - - - - - -
X X X - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
Press any key to continue . . .
CURRENT MOVE IN START OR END POSITION
X - - - - - - - - -
X - - - - - - - - -
X X X - - - - - - -
- - X - - - - - - -
E X X - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
Press any key to continue . . .
X - - - - - - - - -
X - - - - - - - - -
X X X - - - - - - -
- - X - - - - - - -
E X X - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
- - - - - - - - - -
Press any key to continue . . .
D:UsersjdsDocumentsVisual Studio 2010ProjectsWriteFileTestDebug>
OUTPUT FILE: MAP.TXT
X---------X---------XXX---------X-------EXX---------------------------------------------------------