/*
The code below doesnt work as its supposed to. Fix the functions so that they work as the
comments describe and dont produce compiler errors or warnings, nor valgrind errors or memory
leaks. The main function has no problems, and it cannot be changed. You can also assume that
there are no mistakes with the road points and their handling in any way. The structures are also
defined correctly, and no includes are missing. In general, the logic of the code is correct.
The program handles map data, more specifically map tiles. The program saves them in a Map
structure, that holds a location name, a MapTile linked list and the number of tiles in the array.
The MapTile structure holds the data for a single map tile, and stores the id of the map tile, the
center coordinate (a latitude-longitude pair) of the tile and the size and the zoom level for the tile.
There are six mistakes in the program, each of them clearly visible either in the compiler
errors/warnings or valgrind output. Note that there might be more than one error/warning per
mistake as a single mistake can create many problems at once.
There are two mistakes in the createMapTile function.
There is one mistake in the createMapTiles function.
There is one mistakes in the createMap function.
There is one mistake in the printTileInfo function.
There is one mistake in the freeMemory function.
*/
//--------------------------------------------------------------------------
#include
#include
#include
// MapTile structure holds the center location for the map tile,
// and its id, size and zoom
typedef struct mapTile MapTile;
/**
* \brief A map tile structure that specifies a map tile with its center location,
* identifier, size and zoom level.
*
*/
struct mapTile {
double
centerLocation[2]; //!< The center location as a latitude-longitude pair
char id[15]; //!< The tile identifier as a 14 character string
double size; //!< The size of the tile
int zoom; //!< The zoom level
MapTile* next; //!< The next tile in the linked list
};
// Map structure holds the map tile data for the map location
// as a MapTile structure array, as well as the amount of map tiles
// in the array and the name of the location for the map.
typedef struct map {
MapTile* mapTiles; //!< A linked list of tiles
char* locationName; //!< The name of the map that is containing the tiles
} Map;
// Function createMapTile creates a linked list member with the given data.
/**
* \brief Create a MapTile linked list member from the arguments.
*
* \param tileId The tile identifier
* \param zoom The zoom level of the tile
* \param size The size of the tile
* \param centerLocation The center location of the tile
* \return A dynamically allocated and initialized MapTile object
*/
MapTile*
createMapTile(char* tileId, int zoom, double size, double centerLocation[2]) {
// Allocate memory for map tile
MapTile* tile = malloc(sizeof(MapTile));
strcpy(tile->size, size);
tile->zoom = zoom;
// Save the name
tile->id = malloc(strlen(tileId) + 1);
st.
The code below doesn�t work as it�s supposed to. Fix the functi.pdf
1. /*
The code below doesnt work as its supposed to. Fix the functions so that they work as the
comments describe and dont produce compiler errors or warnings, nor valgrind errors or memory
leaks. The main function has no problems, and it cannot be changed. You can also assume that
there are no mistakes with the road points and their handling in any way. The structures are also
defined correctly, and no includes are missing. In general, the logic of the code is correct.
The program handles map data, more specifically map tiles. The program saves them in a Map
structure, that holds a location name, a MapTile linked list and the number of tiles in the array.
The MapTile structure holds the data for a single map tile, and stores the id of the map tile, the
center coordinate (a latitude-longitude pair) of the tile and the size and the zoom level for the tile.
There are six mistakes in the program, each of them clearly visible either in the compiler
errors/warnings or valgrind output. Note that there might be more than one error/warning per
mistake as a single mistake can create many problems at once.
There are two mistakes in the createMapTile function.
There is one mistake in the createMapTiles function.
There is one mistakes in the createMap function.
There is one mistake in the printTileInfo function.
There is one mistake in the freeMemory function.
*/
//--------------------------------------------------------------------------
#include
#include
#include
// MapTile structure holds the center location for the map tile,
// and its id, size and zoom
typedef struct mapTile MapTile;
/**
* brief A map tile structure that specifies a map tile with its center location,
* identifier, size and zoom level.
*
*/
struct mapTile {
double
centerLocation[2]; //!< The center location as a latitude-longitude pair
char id[15]; //!< The tile identifier as a 14 character string
2. double size; //!< The size of the tile
int zoom; //!< The zoom level
MapTile* next; //!< The next tile in the linked list
};
// Map structure holds the map tile data for the map location
// as a MapTile structure array, as well as the amount of map tiles
// in the array and the name of the location for the map.
typedef struct map {
MapTile* mapTiles; //!< A linked list of tiles
char* locationName; //!< The name of the map that is containing the tiles
} Map;
// Function createMapTile creates a linked list member with the given data.
/**
* brief Create a MapTile linked list member from the arguments.
*
* param tileId The tile identifier
* param zoom The zoom level of the tile
* param size The size of the tile
* param centerLocation The center location of the tile
* return A dynamically allocated and initialized MapTile object
*/
MapTile*
createMapTile(char* tileId, int zoom, double size, double centerLocation[2]) {
// Allocate memory for map tile
MapTile* tile = malloc(sizeof(MapTile));
strcpy(tile->size, size);
tile->zoom = zoom;
// Save the name
tile->id = malloc(strlen(tileId) + 1);
strcpy(tile->id, tileId);
// Save the center coordinate
tile->centerLocation[0] = centerLocation[0];
tile->centerLocation[1] = centerLocation[1];
// Set the next pointer
tile->next = NULL;
return tile;
3. }
// Function createMapTiles takes five parameters:
// - tileIds, an array of tile ids (strings)
// - zoom, the zoom level of map tiles
// - size, the width and height of a map tile
// - centerLocations, and array of pair of doubles
// (latitude-longitude pairs)
// - tileCount, and integer that tells how many tiles
// there are in the arrays
// The function returns a MapTile structure linked list created
// from the data given as parameters
/**
* brief Creates a linked-list of MapTiles
*
* param tileIds An array of tile identifier strings
* param zoom The zoom level of the tile
* param size The size of the tile
* param centerLocations An array of a pair of doubles (latitude-longitude pairs)
* param tileCount The number of tiles in the map
* return A dynamically allocated linked-list of map tiles, and initialized
* using the function arguments.
*/
MapTile* createMapTiles(char** tileIds,
int zoom,
double size,
double centerLocations[][2],
int tileCount) {
MapTile* tile = createMapTile(tileIds[0], zoom, size, centerLocations[0]);
MapTile* start = *tile;
// Go through all the given data and store it in the array
for(int i = 1; i < tileCount; i++) {
tile->next = createMapTile(tileIds[i], zoom, size, centerLocations[i]);
tile = tile->next;
}
return start;
}
4. /**
* brief Create a Map object from the function arguments.
*
* param locationName The name of the map location
* param tileIds An array tile identifier strings
* param zoom The zoom level of tiles
* param size The size of the tiles
* param centerLocations An array of a pair of doubles (latitude-longitude pairs)
* param tileCount The number of tiles in the map
* return A dynamically allocated and initialized map object
*/
Map* createMap(char* locationName,
char** tileIds,
int zoom,
double size,
double centerLocations[][2],
int tileCount) {
// Allocate memory for the Map structure
Map* map = malloc(sizeof(int*));
// Store the location name
map->locationName = malloc(strlen(locationName) + 1);
strcpy(map->locationName, locationName);
// Store the map tiles information
map->mapTiles =
createMapTiles(tileIds, zoom, size, centerLocations, tileCount);
return map;
}
/**
* brief Prints the content of a Map object
*
* details This function prints the content of a Map object in the following
* format:
*
* has the following map tiles:
* Map tile id (size , zoom ) is at (latitude, longitude).
* ...
5. *
* param map The map object to be printed
*/
void printTileInfo(Map* map) {
printf("%s has the following map tiles:n", map->locationName);
// Loop through the map tiles and print their info
for(MapTile current = map->mapTiles; current; current = current->next) {
printf("Map tile id %s (size %.2f, zoom %d) is at (%f, %f).n",
current->id,
current->size,
current->zoom,
current->centerLocation[0],
current->centerLocation[1]);
}
}
/**
* brief Frees the dynamically allocated memory for a map object
*
* param map The object to be freed
*/
void freeMemory(Map* map) {
for(MapTile* current = map->mapTiles; current;) {
MapTile* del = current;
current = current->next;
del = NULL;
}
free(map->locationName);
free(map);
}
/**
* brief Main function of the program
*
* return Returns 0
*/
int main() {
// Original data for map tiles:
6. // Ids
char* ids[] = {"17-74574-37927",
"17-74574-37928",
"17-74574-37929",
"17-74573-37927",
"17-74573-37928",
"17-74573-37929"};
// Size is always the same at the same zoom level
double size = 152.0819;
int zoom = 17;
// Tile center location, a latitude-longitude pair
double centerLocations[][2] = {{60.1859156214031, 24.8249816894531},
{60.1845500274125, 24.8249816894531},
{60.1831843766236, 24.8249816894531},
{60.1859156214031, 24.8222351074219},
{60.1845500274125, 24.8222351074219},
{60.1831843766236, 24.8222351074219}};
// Create a map structure based on the data
Map* map = createMap("Otaniemi", ids, zoom, size, centerLocations, 6);
// Print map tiles information
printTileInfo(map);
// Free the reserved memory
freeMemory(map);
return 0;
}