For this program you will be given a list of city names and x, y coordinates tha
ID: 3574174 • Letter: F
Question
For this program you will be given a list of city names and x, y coordinates that signify the location of the city. You will store the list of cities in an array of structures and then use that array to write a series of functions.The data will be in a text file named "route_data.txt" and will be in the following format:
city x_coord y_coord
e.g.
Houston 0.5 1.6
Denver -1.5 4.2
etc.
The cities are in order. The first city is your home city. You will figure out how far it is from your home city to the last city in the list by moving from city to city in the list and calculating the total distance (we are assuming that we have cars that fly through the air and can use a straight line distance). Then you will write a function to improve the ability to visit each city once (the last city you visit may change).
You will write four functions for this program:
//reads the initial data from the input file
//returns false if the file is not opened correctly
bool getData(string inputFile, City cities[], int &numCities);
//prints the cities in order, one per line
void printRoute(City cities[], int numCities);
//calculates the distance from one city to the next
double getDistance(City city1, City city2);
//calculates the total distance from the first city to the last city
double totalDistance(City cities[], int numCities);
//uses an algorithm to make the total distance shorter than the initial distance (if possible)
void improveRoute(City cities[], int numCities);
Your main program should have the following algorithm:
get the data
print the route
show the total distance
improve the route
print the route
show the total distance
display the percentage improvement
We will discuss the details of the assignment in class, including a simple route improving algorithm (although you may come up with something better on your own).
NOTES:
The program must be modular, with significant work done by functions. Each function should perform a single, well-defined task. When possible, create re-usable functions.
We have provided the prototypes for the functions you will need.
There will be a maximum of 100 cities.
Explanation / Answer
PROGRAM CODE:
#include <iostream>
#include <cmath>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
struct City
{
string cityName;
double x;
double y;
bool isVisited = false;
};
//reads the initial data from the input file
//returns false if the file is not opened correctly
bool getData(string inputFile, City cities[], int &numCities);
//prints the cities in order, one per line
void printRoute(City cities[], int numCities);
//calculates the distance from one city to the next
double getDistance(City city1, City city2);
//calculates the total distance from the first city to the last city
double totalDistance(City cities[], int numCities);
//uses an algorithm to make the total distance shorter than the initial distance (if possible)
void improveRoute(City cities[], int numCities);
int main() {
City cities[100];
int citiesnum = 3;
getData("string inputFile", cities, citiesnum);
printRoute(cities, 3);
cout<<"Total Distance : "<<totalDistance(cities, citiesnum);
improveRoute(cities, citiesnum);
printRoute(cities, 3);
return 0;
}
bool getData(string inputFile, City cities[], int &numCities)
{
string name;
bool isRead = false;
double x_c,y_c;
ifstream infile(inputFile);
string line;
while(gets(infile, line))
{
istringstream iss(line);
if(!(iss>>name>>x_c>>y_c))
{
}
cities[numCities].cityName = name;
cities[numCities].x = x_c;
cities[numCities].y = y_c;
cities[numCities].isVisited = true;
numCities++;
isRead = true;
}
return isRead;
}
void printRoute(City cities[], int numCities)
{
for(int i=0; i<numCities; i++)
{
if(cities[i].isVisited)
cout<<cities[i].cityName<<" "<<to_string(cities[i].x)<<" "<<to_string(cities[i].y)<<endl;
}
}
double getDistance(City city1, City city2)
{
return sqrt(pow((city2.x-city1.x),2) + pow((city2.y-city1.y),2));
}
double totalDistance(City cities[], int numCities)
{
double totalDistance = 0;
for(int i=0; i<numCities-1; i++)
{
totalDistance += getDistance(cities[i], cities[i+1]);
}
return totalDistance;
}
void improveRoute(City cities[], int numCities)
{
double min = 0;
double totalDistance = 0;
for (int i = 0; i < numCities; ++i)
{
for (int j = i+1; j < numCities; ++j)
{
if (getDistance(cities[i], cities[j]) < min)
min = getDistance(cities[i], cities[j]);
}
totalDistance += min;
}
}
OUTPUT:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.