Parsing Arrays in C++An array can be defined as a data structure consisting of a group of elements. Basically, all these elements have the same data type, for example, a string or integer. Arrays are used in C++ and other programming languages to organize data or information, so that, values and var...
In this assignment sample, I am going to show you how to use C++ to create a console-based program that helps navigate a given college’s buildings via footways. We are going to build a graph class that uses an adjacency list representation. We will then use Dijkstra’s algorithm to determine the shortest path around the buildings.
Solution
#include
#include /*setprecision*/
#include
#include
#include
#include
#include
#include
#include "tinyxml2.h"
#include "dist.h"
#include "osm.h"
#include"graph.h"
#include"algs.h"
using namespace std;
using namespace tinyxml2;
// This function takes a building and the current nodes and
// footways and search for the nearest node in the nodes
// of the foot ways. Returns the id of the nearst node.
long longgetNearestNode(BuildingInfo building, map&Nodes,vector& footway) {
double minNodeDest = 2e10;
long longminNodeID = -1;
double dist;
for (int i = 0; i
// searching in each footway
for (size_t j = 0; j < footway[i].Nodes.size(); j++) {
// search in each node in the foot way
dist = distBetween2Points(building.Coords.Lat, building.Coords.Lon
, Nodes[footway[i].Nodes[j]].Lat, Nodes[footway[i].Nodes[j]].Lon);
if (dist
minNodeDest = dist;
minNodeID = footway[i].Nodes[j];
}
}
}
return minNodeID;
}
// This function prints the path recursively by going to
// the calling the funciton for parent then print.
void printPath(long long id, map&preds) {
if (id == -1) {
return;
}
printPath(preds[id], preds);
cout<< id << "->";
}
int main() {
// maps a Node ID to it's coordinates (lat, lon)
map Nodes;
// info about each footway, in no particular order
vector Footways;
// info about each building, in no particular order
vector Buildings;
XMLDocumentxmldoc;
cout<< "** Navigating UIC open street map **" <
cout<
cout<
string def_filename = "map.osm";
string filename;
cout<< "Enter map filename> ";
getline(cin, filename);
if (filename == "") {
filename = def_filename;
}
//
// Load XML-based map file
//
if (!LoadOpenStreetMap(filename, xmldoc)) {
cout<< "**Error: unable to load open street map." <
cout<
return 0;
}
//
// Read the nodes, which are the various known positions on the map:
//
int nodeCount = ReadMapNodes(xmldoc, Nodes);
//
// Read the footways, which are the walking paths:
//
int footwayCount = ReadFootways(xmldoc, Footways);
//
// Read the university buildings:
//
int buildingCount = ReadUniversityBuildings(xmldoc, Nodes, Buildings);
//
// Stats
//
assert(nodeCount == (int)Nodes.size());
assert(footwayCount == (int)Footways.size());
assert(buildingCount == (int)Buildings.size());
cout<
cout<< "# of nodes: " <
cout<< "# of footways: " <
cout<< "# of buildings: " <
//
// TO DO: build the graph, output stats:
//
graph G;
// Add Vertices to the graph
auto iter = Nodes.begin();
while (iter != Nodes.end()) {
G.addVertex(iter->first);
iter++;
}
// add the edges to the graph
for (size_ti = 0; i
for (size_t j = 1; j < Footways[i].Nodes.size(); j++) {
long long from = Footways[i].Nodes[j];
long long to = Footways[i].Nodes[j - 1];
double dist = distBetween2Points(Nodes[to].Lat
, Nodes[to].Lon, Nodes[from].Lat, Nodes[from].Lon);
// adding an edge forth and back
G.addEdge(from, to, dist);
G.addEdge(to, from, dist);
}
}
cout<< "# of vertices: " <
cout<< "# of edges: " <
cout<
//
// Navigation from building to building
//
// Defining variable needed to navigate
string startBuilding_str, destBuilding_str;
BuildingInfostartBuilding, destBuilding;
long longstartNode, destNode;
// maps and vectors for dijkstra
mapdists;
mappreds;
vectordistances_res;
cout<< "Enter start (partial name or abbreviation), or #> ";
getline(cin, startBuilding_str);
while (startBuilding_str != "#") {
cout<< "Enter destination (partial name or abbreviation)> ";
getline(cin, destBuilding_str);
//
// TO DO: lookup buildings, find nearest start and dest nodes,
// run Dijkstra's alg, output distance and path to destination:
//
// first search with exact abbreviation
startBuilding.Coords.ID = destBuilding.Coords.ID = -1;
for (size_ti = 0; i
if (Buildings[i].Abbrev == startBuilding_str) {
startBuilding = Buildings[i];
}
if (Buildings[i].Abbrev == destBuilding_str) {
destBuilding = Buildings[i];
}
}
// if any of the nodes not found, then search for partial match in names
for (size_ti = 0; i
if (startBuilding.Coords.ID == -1
&& Buildings[i].Fullname.find(startBuilding_str) != -1) {
startBuilding = Buildings[i];
}
if (destBuilding.Coords.ID == -1
&& Buildings[i].Fullname.find(destBuilding_str) != -1) {
destBuilding = Buildings[i];
}
}
if (startBuilding.Coords.ID == -1) {
cout<< "Start building not found" <
} else if (destBuilding.Coords.ID == -1) {
cout<< "Destination building not found" <
}
if(startBuilding.Coords.ID != -1 && destBuilding.Coords.ID != -1) {
// start and destination are valid buildings
cout<< "Starting point:" <
cout<< " " <
cout<< " (" <
<< ", " <
cout<< "Destination point:" <
cout<< " " <
cout<< " (" <
<< ", " <
cout<
// finding the neraest nodes
startNode = getNearestNode(startBuilding, Nodes, Footways);
destNode = getNearestNode(destBuilding, Nodes, Footways);
cout<< "Nearest start node:" <
cout<< " " <
cout<< " (" << Nodes[startNode].Lat<< ", "
<< Nodes[startNode].Lon<< ")" <
cout<< "Nearest destination node:" <
cout<< " " <
cout<< " (" << Nodes[destNode].Lat<< ", "
<< Nodes[destNode].Lon<< ")" <
cout<
// use dijkstra
distances_res = Dijkstra(G, startNode, dists, preds);
cout<< "Navigating with Dijkstra..." <
if (dists[destNode] == INF) {
cout<< "Sorry, destination unreachable" <
} else {
cout<< "Distance to dest: " <
cout<< "Path: ";
printPath(preds[destNode], preds);
cout<
}
}
//
// another navigation?
//
cout<
cout<< "Enter start (partial name or abbreviation), or #> ";
getline(cin, startBuilding_str);
}
//
// done:
//
cout<< "** Done **" <
return 0;
}
/*! \file prog1.cpp
\brief Assignment 1 program
The program asks for input from the user and displays the read number as a
IEEE-754 floating point number in binary form.
*/
#include
#include
#include
/*! \fn void printBinFloat(int32_t x)
\brief Prints the given number as a IEEE-754 floating point number
\param num Number to be printed.
The function prints the number in hexadecimal, in binary and then prints
the IEEE-754 field values. After that, it prints the decoded floating point
number in binary.
*/
void printBinFloat(int32_t num)
{
// print hexadecimal number
std::cout << "0x" << std::hex << std::setw(8) << std::setfill('0') << num << " = ";
// print binary number
int tmpnum = num;
for (int i = 0; i < 8; i++) // print 8 nibbles
{
for (int i = 0; i < 4; i++) // print 4 bits
{
std::cout << (tmpnum & 0x80000000 ? '1':'0');
tmpnum <<= 1;
}
std::cout << " ";
}
std::cout << std::endl;
int sign = (num & 0x80000000) ? 1 : 0; // get the sign bit (31)
int exp = (num >> 23) & 0xFF; // get the 8 exponent bits from 23 to 30
exp -= 127; // remove bias
uint32_t sig = num & 0x7FFFFF; // get the 23 bits of significand
// print floating point fields
std::cout << "sign: " << sign << std::endl;
std::cout << " exp: 0x" << std::hex << std::setw(8) << std::setfill('0') << exp << " (" << std::dec << exp << ")" << std::endl;
std::cout << " sig: 0x" << std::hex << std::setw(8) << std::setfill('0') << sig << std::endl;
// print floating point number
std::cout << ((sign)? '-' : '+'); // print sign
if (exp == 128 && sig == 0) // infinite
{
std::cout << "inf";
}
else if (exp == -127 && sig == 0) // zero
{
std::cout << "0";
}
else // floating point value
{
// add 1 to significand
sig |= 0x800000;
// print initial zeros if negative exponent
for (int j = exp; j <= -1; j++)
{
std::cout << "0";
if (j == exp)
std::cout << ".";
}
// print 24 bits of significand
for (int i = 0; i < 24; i++)
{
std::cout << (sig & 0x800000 ? '1':'0');
if (i == exp)
std::cout << ".";
sig <<= 1;
}
// print end zeros if positive exponent
for (int j = 23; j <= exp; j++)
{
std::cout << "0";
if (j + 1 == exp)
std::cout << ".";
}
}
std::cout << std::endl;
}
/*! \fn int main()
\brief Main function
The main function for the program prompts user for a number and prints it
using the printBinFloat function. It loops until the user decides to end
the program.
*/
int main()
{
while(true)
{
uint32_t number;
std::cout << "Number? ";
std::cin >> std::hex >> number;
printBinFloat(number);
std::cout << "End program (Y/N)? ";
char quit;
std::cin >> quit;
if (quit == 'Y' || quit == 'y')
break;
std::cout << std::endl;
}
return 0;
}